The difference between using and using a pipe in Export-Csv in Powershell

This is probably more “how PowerShell handles variables and pipes” than a specific programming question, but since it seems to me that this is a strange behavior (to me), I thought I would post it here.

I just had difficulty exporting the variable to CSV using PowerShell and found this question on Stack that helped me a lot . However, when I was busy with the exit, I got two different results, depending on what I called the function Export-CSV.

I have a custom PS object that looks something like this:

Account      Partner     ProjectName     ProjectPhase
1            A           Test            Start
2            B           Test2           Start
3            A           Test4           End
4            C           Test3           Middle
....

When I use the following line, it outputs the CSV file correctly:

$csvBody | Export-Csv -Path "$targetPath\$fileName" -Encoding Unicode -NoTypeInformation

However, when I use the following line, this is not the case:

Export-Csv -InputObject $csvBody -Path "$targetPath\$fileName" -Encoding Unicode -NoTypeInformation

:    "", "LongLength", "", "SyncRoot", "IsReadOnly", "IsFixedSize", "IsSynchronized"   263, "263", "263", "1", "System.Object []", "", "True", "False"

Stack, $csvBody, . : , Export-CSV, , ?

+4
2

:

#TYPE System.Object[], , TypeInformation.

Export-Csv , .


:

$a = 1..4

:

$a | Get-Member

: System.Int32

:

Get-Member -InputObject $a

: System.Object[]

, - , .

+3

, , Export-CSV , ( ) .

, , , , -InputObject . :

   Export-Csv [[-Path] <String>] [[-Delimiter] <Char>] [-Append] [-Confirm] [-Encoding <String> {Unicode | UTF7 |
   UTF8 | ASCII | UTF32 | BigEndianUnicode | Default | OEM}] [-Force] -InputObject <PSObject> [-LiteralPath <String>]
   [-NoClobber] [-NoTypeInformation] [-WhatIf] [<CommonParameters>]

<psobject>, <psobject[]>, <psobject>. -, , , CSV, ( ..).

, cmdlet PROCESS .

-, , , , , , , Export-CSV , ( - - ).

+5

Source: https://habr.com/ru/post/1676149/


All Articles