D.chan D.chan - 3 months ago 24
PowerShell Question

PowerShell csv character replacement

I manage to generate a csv through PowerShell script on collecting a group of server disk info, but the result output on the csv file require some data massage.

below will be the script for ref:

foreach($pc in $comp)
$diskvalue += Get-WmiObject @Params | Select @{l='drives';e='DeviceID'}, @{l='server',e='SystemName'}, @{Name=”size(MB)”;Expression={“{0:N1}” -f($_.size/1mb)}}, @{Name=”freespace(MB)”;Expression={“{0:N1}” -f($_.freespace/1mb)}}, @{Name=”UsedSpace(MB)”;Expression={“{0:N2}” -f(($_.size - $_.FreeSpace)/1mb)}}
$diskvalue | Export-Csv C:\disk_info\DiskReport.csv -NoTypeInformation


The output csv file on "drives" column will contain:


C:


Yet I would like to remove the ":" at the back that data output.


C

Answer

Change:

@{l='drives';e='DeviceID'}

to

@{l='drives';e={"$($_.DeviceID)".Trim(": ")}

Trim(": ") will remove any whitespace and : characters from the DeviceID string