I found strange behavior using powershell.
The output of only quotation marks is different from the result of the ToString () method called on the same object.
which method or property is called in
Write-Output "$a"
This is the directory in which I run the example
> PS C:\temp\testps> DIR Directory: C:\temp\testps Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 03/12/2010 11.21 8 1.txt -a--- 03/12/2010 11.21 8 2.txt -a--- 03/12/2010 11.21 8 3.txt
Here I assign a collection of files to the $ q object
PS C:\temp\testps> $q = Get-ChildItem . "*.txt"
Now I want to print the file names, but I get unexpected output
PS C:\temp\testps> foreach ($a in $q) {Write-Output $a} Directory: C:\temp\testps Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 03/12/2010 11.21 8 1.txt -a--- 03/12/2010 11.21 8 2.txt -a--- 03/12/2010 11.21 8 3.txt
Putting quotes around the object name shows the correct behavior
PS C:\temp\testps> foreach ($a in $q) {Write-Output "$a"} 1.txt 2.txt 3.txt
This result is only for the first object. It seems that the entire DIR command is output to the first file name. Disassembled.
PS C:\temp\testps> foreach ($a in $q) {Write-Output $a;break} Directory: C:\temp\testps Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 03/12/2010 11.21 8 1.txt
The ToString () method behaves like a quoted object.
PS C:\temp\testps> foreach ($a in $q) {Write-Output $a.ToString()} 1.txt 2.txt 3.txt
source share