D.chan D.chan - 1 year ago 174
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:


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


Answer Source




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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download