Formatting tables in PowerShell to export as CSV - powershell

I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services

PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture

Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation

Related

Export to CSV w/o Header or Quotes

I need to create a new CSV from an existing table, but I need to add a column before and have no headers, or quotes around the data within the cells. I noticed if I open the file, save it, quotes are removed but this file will be going external at 1AM nightly
Example Example in Windows Explorer
$NewCSV = '\\server2\export.csv'
$Assets = (import-csv '\\server1\PCList.csv' | Where-Object {$_.PCNumber -ne ''}).PCnumber | ForEach-Object {
[pscustomobject]#{
'h1' = 'IT Support'
'h2' = $_+'.acme.net'
}
} | Export-Csv $NewCSV
I have seen usage of Outfile but that breaks my columns
If you're running PowerShell (Core) 7+:
... |
ConvertTo-Csv -UseQuotes Never |
Select-Object -Skip 1 |
Set-Content $NewCSV
If you're running Windows PowerShell:
... |
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
ForEach-Object { $_ -replace '"' } |
Set-Content $NewCSV
Note:
ConvertTo-Csv is the in-memory equivalent of Export-Csv, which therefore allows skipping the first output object, representing the CSV header, via Select-Object.
Set-Content can then be used to save the results to a file. Use its -Encoding parameter to control the character encoding.
In Windows PowerShell only, -NoTypeInformation is additionally needed to suppress the usually undesired type-annotation line that is emitted before the header line; the same goes for Export-Csv.
The following applies to both ConvertTo-Csv and Export-Csv:
In Windows PowerShell, all CSV column values are invariably double-quoted (enclosed in "..."), but in PowerShell (Core) 7+ you can control the double-quoting behavior via the -UseQuotes parameter. Additionally, you can choose to selectively double-quote columns (fields), via the -QuoteFields parameter.

How do I export these Active Directory User Properties to CSV? [duplicate]

I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services
PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture
Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation

Query regarding the format of a csv file when using the export-csv command in powershell

I'm familiar with exporting data to csv with Powershell, but a particular vendor wants to receive the data in a file that is specific type 'CSV'. If I use the following example command, the data looks fine in the raw csv format (i.e via notepad), where you see it's comma separated ...
,,JVF1033292,test,SL,10700,6804626,745.586,43001843,Test,8/12/2020,8/14/2020,T,5584,,,JPY,0,XTKS,,,,,,0
,,JVF1033293,test,SL,3695,6805179,1362.8457,43001843,Test,8/12/2020,8/14/2020,T,3524,,,JPY,0,XTKS,,,,,,0
... but when the same file is opened in Excel all the data is in one row and therefore is failing on the vendors side. The code I'm using is below.
$Table | ConvertTo-Csv -NoTypeInformation | ForEach-Object {$_-replace "`"", ""} | select -skip 1 | out-file -filepath ("$dir_opr\LVT\Export\CTO\ForJefferies\GMO_Jefferies_Trade_Data_" + $Date+ ".csv")
If I use the below code, then it looks fine in Excel (tabbed correctly), but the raw file is also tabbed and not comma separated which the vendor has issues with.
$Table | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | ForEach-Object {$_-replace "`"", ""} | select -skip 1 | out-file -filepath ("$dir_opr\LVT\Export\CTO\ForJefferies\GMO_Jefferies_Trade_Data_" + $Date+ ".csv")
X JVF1032244 Test BY 450.0000 BYM41J3 10.00000000 43001843 Test 08/11/2020 08/13/2020 T 3.00 JPY 0 XTKS 0.00
X JVF1032245 Test BY 200.0000 BYM41J3 250.00000000 43001843 Test 08/11/2020 08/13/2020
Is it possible to create a comma separated and tabbed raw file, that also is delimited in Excel so not all in one column?
As far as I can tell with testing locally, it might be due to the default encoding that out-file uses - i.e utf8NoBOM. (See the description for the -Encoding parameter for details: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7)
Try out-file -filepath $myfile -encoding utf8 - that seems to fix it on my machine.

Powershell Output File column too narrow

I am trying to list current installed applications and the DisplayVersion keeps getting its column shortened when I want it to show as normal on one line. It runs fine when running the code in a PS session but when running from a ps1 script file, the output for DisplayVersion is 1 character wide with NewLines for each integer.
$FormatEnumerationLimit = -1
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Publisher, DisplayName, DisplayVersion |
Where {$_.DisplayName} |
Sort-Object -Property Publisher,DisplayName,DisplayVersion |
Format-Table -AutoSize -Wrap > $FullFileName
Considering the exact suggestion from LotPings, I am making this as an answer because it makes more sense to dump it in File using out-file & use Encoding instead of redirection.
Replace this:
Format-Table -AutoSize -Wrap > $FullFileName
To:
Out-File $FullFileName -Width 300 -Encoding Ascii

Running a PowerShell script in SQL Server Agent is not working

I have a SQL Server Agent job that runs a PowerShell script to pull data from a database and save it in a csv file.
The job type is set to PowerShell and the command looks like this:
$sql=#'exec my_stored_procedure'#
$result = Invoke-Sqlcmd -ServerInstance INSTANCE_NAME -Database DBNAME -Query $sql
#The first row gets removed and then save it to a csv file
$result | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content \\SERVER\c$\Folder\file.csv
When I run this job, it runs successfully, but the file.csv is not created in the specified location.
I checked the Job History and it says:
The job script encountered the following errors. These error did not
stop the script: (.....) The corresponding line is $result |
ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content
\\SERVER\c$\Folder\file.csv. Correct the script and reschedule the
job. The error information returned by PowerShell is 'Cannot use
interface. The IContentCmdletProvider interface is not implemented by
this provider.'. Process Exit Code 0.
Could someone help me understand what I'm missing?
This is my first PowerShell project, so I'm probably missing things that are obvious to other people..?
It seems that it was complaining because of the server name in the path.
I originally had:
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content \\SERVER\c$\Folder\file.csv
but CDing to the directory and specifying the file did the trick:
$directoryPath = "C:\Folder"
cd $directoryPath
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content C:\Folder\file.csv