Get-Process | Export-Csv -path C:\temp\Process.csvThis above one-line script gets all the process and their usage and writes into an Excel CSV file in C:\temp\Process.csv
Function Get-Devicesize { param($ComputerName) # you need pass your remote machine name when you use this function $drive = Get-WmiObject Win32_logicaldisk -Filter 'Deviceid = "c:"' $properties = @{ ComputerName = $ComputerName FreeSpace = $drive.FreeSpace DeviceID = $drive.DeviceID Size = $drive.Size } New-Object -TypeName PSobject -Property $properties }Let’s run this function and export!
Get-Devicesize -ComputerName DESKTOP-Win10 | Export-Csv -Path C:\temp\deviceinfo.csv -Force -NoTypeInformation -AppendOutput: While running the function we used -NoTypeInformation parameter, this omits writing the object type information on top of the Excel CSV file. Generally, we don’t use this object type information of any output (Powershell commands). Let’s Learn How to Import or Read data from CSV file to Powershell Script There are two commands available in powershell to import the data into scripts, one is Import-Csv and another one is Get-Content, both work technically same, however, the command Import-Csv reads the data structured Csv file format, but not a simple text file. Syntax:
Example:
Import-Csv -Path C:\temp\deviceinfo.csvIf you observe closely, we are importing the Excel CSV file that we were exported in our example of the above Powershell function. OutPut: FreeSpace Size ComputerName DeviceID ——— —- ———— ————————————- 51386757120 106820530176 DESKTOP-Win10 C: Thank you for reading this article, if you have any questions please let us know. Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply