r/PowerShell • u/mikeymikeymikec • May 15 '26
Question PS newbie here, either PS has a weird problem with arrays or I'm doing something wrong
I'll start as simply as possible. I want to create a string with a file path in, for example:
PS C:\Users\Owner> $test = $ENV:USERPROFILE + '\NTUSER.DAT'
PS C:\Users\Owner> $test
C:\Users\Owner\NTUSER.DATCorrect.
PS C:\Users\Owner> $testarray = @($ENV:USERPROFILE + '\NTUSER.DAT')
PS C:\Users\Owner> $testarray
C:\Users\Owner\NTUSER.DATStill correct.
PS C:\Users\Owner> $testarray = @($ENV:USERPROFILE + '\NTUSER.DAT', $ENV:LOCALAPPDATA + '\Microsoft\OneDrive')
PS C:\Users\Owner> $testarray
C:\Users\Owner\NTUSER.DAT C:\Users\Owner\AppData\Local\Microsoft\OneDriveStill correct.
PS C:\Users\Owner> $testarray = @('Alwil Software', $ENV:USERPROFILE + '\NTUSER.DAT',$ENV:LOCALAPPDATA + '\Microsoft\OneDrive')
PS C:\Users\Owner> $testarray
Alwil Software
C:\Users\Owner
\NTUSER.DAT
C:\Users\Owner\AppData\Local
\Microsoft\OneDriveSay what now? Help! It does the same thing in the latest version of PS (Windows).
5
Upvotes
22
u/thomsxD May 15 '26
Because PS treats "+" as literal in an array and commas as array separators.
11
u/purplemonkeymad May 15 '26
This is because '+' takes the type from the left hand side. In addition ',' is an operator.
Powershell parses left to right and these operators have equal presidence. So in:
'+' comes first, and the left side is a string, so it will output a string as a result.
In:
a ',' comes first. That operator will take both sides and you'll get an array as an output. Next is '+' which is now given an array on the left. This means it will do an array addition with the right giving you an array as the output:
As suggested use parenthesis to tell powershell the order of operations:
6
u/Grouhl May 15 '26
Doesn't answer your question, but using Join-Path instead of just plain ol' string concatenation is better when putting paths together. And I'm betting in this instance it would conveniently side-step the issue.
It I were to hazard a guess as to whatever the heck's going on there, it might be prioritizing the operators in a weird way and treating it as arrays being added together. Try putting parentheses around the string concatenations (the second and third elements in example 3) and see if that doesn't make it behave right.
5
u/y_Sensei May 15 '26 edited May 15 '26
It's actually a matter of order of operation when evaluating expressions, in combination with operator precedence.
The documentation states:
and also:
So in your scenario,
+is being used to concatenate Strings, which takes precedence over element allocation with,even in an expression that's supposed to evaluate to an array (ie@(...)), because that final evaluation has an even lower precedence. You can (and should) of course change this default behavior by using round brackets in the appropriate places, as already suggested by other users in this thread.Check this:
2
u/BlackV May 15 '26 edited May 15 '26
In addition to what suppaduppasleuth said, try instead
ideally you should not concatenate strings use the
'xxx' + 'yyy'2
u/TofuBug40 May 15 '26
Ok this comes down to how the parser is actually interpreting things.
You are probably used to + as a concatenation operator and it totally can be but here its contexctual and means a separator just like ,
To PowerShell
@('Hello','Hello', 'World', 'Hello' + 'World')is the same as
incidentally if you stick to one line per item the commas are optional the line breaks are the separators
if you want it to work the way you have it you have to EXPLICITLY force it to do the concatenation first by
What you really should be reaching for is Everything about string substitutions
then you get
Subtle difference but if you are going to thrive in PowerShell you HAVE to understand the myriad of ways PowerShell treats strings there is a lot of power and flexibility but you can get tripped up easily.
1
u/Fit_Indication_2529 May 15 '26
the comma splits array elements before + finishes building your strings, so wrap the + expressions in parentheses or use interpolation/Join-Path
-1
u/suppaduppasleuth May 15 '26
Dude, your array is fine. PowerShell's just being weird with how it displays stuff.
Here's what's actually going on: when you've got 3 or fewer items, PowerShell shows them on one line separated by spaces. When you have more items OR if the strings are really long, it switches to showing one item per line. But here's the kicker - it's ALSO wrapping long strings based on your console width, which makes it look like your paths got chopped up into separate array elements. They didn't.
Try this:
The array itself is completely fine. PowerShell's console output just sucks sometimes and makes it look broken when it's not.
If you want to see what's actually in the array without the BS formatting:
Trust me, you're not crazy. This trips up everyone at some point.Dude, your array is fine. PowerShell's just being weird with how it displays stuff.