0

error screenshot

I just wanted to run a simple command like echo @profile, but the output is vertical. I can theoretically read and understand the output, but it is a big unconvenience. How can I fix it?

  • If you want to output your profile path simply output the variable as it is: `$profile` – Olaf Oct 18 '21 at 18:23
  • 4
    You want `echo $profile`, not `echo @profile`. [Splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.1#splatting-with-arrays) a string value (like `$profile`) causes powershell to turn it into an array and as a result `echo` outputs each character on a new line. – Mathias R. Jessen Oct 18 '21 at 18:23

1 Answers1

3

You don't normally reference variables with an @ symbol, you almost always use the $ to reference a variable's value by the variable name. You can also use the Variable: provider or Get-Variable, but I won't get into those here.

If you were to omit echo, you would actually get the following error message:

The splatting operator '@' cannot be used to reference variables in an expression.
'@var' can be used only as an argument to a command. To reference variables
in an expression use '$var'.

This is because using @var is a technique called Splatting, which is the practice of using an array or hashtable to provide the arguments to a command, function, or cmdlet. Note you cannot currently splat arguments to methods. Review my answer linked above for more information on how to actually splat arguments in several use cases.

As for why you get the vertical output, note that echo is actually an alias to Write-Output. Write-Output accepts positional arguments, for which it will output each object passed in on its own line. When you splat a string as an array of arguments to a function, it converts the string to an array of characters, so effectively you are passing in each character of @profile to Write-Output as its own argument, then spitting each element of the array back out. And when PowerShell displays an array directly to the console, it displays each element on its own line.

Note: Of the different Write- cmdlets, Write-Output is unique in that it will output each positional parameter on its own line as it returns an array for each argument you pass in. The other Write- cmdlets will instead join each element of the array into a single space-delimited string. The Write- cmdlets come into play when working with the different output streams in PowerShell. Here is another answer of mine which explains what the different output streams mean and how to write to them.

In addition, for displaying purely information text to the end user that does not need additional programmatic processing in your script or session, use Write-Host.

This is why you get the vertical output, because @profile is being converted to an array, then splatted into Write-Output as an array of characters, and Write-Output will write back all arguments of an array as individual elements of a new array. PowerShell will then display the new array with each element on its own line.


I suspect what you actually want to do is output $profile. You can use one of the following techniques (note that echo/Write-Output are often redundant to use):

# Use the alias
echo $profile

# Use Write-Output
Write-Output $profile

# Omit Write-Output entirely
$profile

# View one of the alternative profiles by name
# CurrentUserCurrentHost is the default
# and is most often the one you are looking for
$profile.CurrentUserCurrentHost
$profile.CurrentUserAllHosts
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts
codewario
  • 19,553
  • 20
  • 90
  • 159