Useful PowerShell Commands
Revision as of 19:24, 18 October 2024 by Bacchas (talk | contribs) (→Sorting Object in the pipeline)
Get-ComputerInfo
Accessing the properties of a command
Get-Computerinfo -Properties csusername Get-Computerinfo -Properties csusername | Get-Member Get-ComputerInfo | Format-List OsType,osName,csusername
Write output to the screen, like Echo
Write-Output "Hello World"
Read Text file
Get-Content filename.txt
Rename-Computer Restart-Computer Get-Date Get-TimeZone
Update Windows From PowerShell
Import csv file The pipe it to another function
import-csv .\filename.csv |
Rename Files using csv list / excel file (exported to CSV)
If you execute the script in the same directory as the files, you don't have to add the full path, but if it's in a different directory the the full path is needed
You can start with an excel file. Header has to be Path and NewName
| Path | NewName |
|---|---|
| cat.jpg | cat01.jpg |
| dog.jpg | dog02.jpg |
| rat.jpg | rat03.jpg |
Path refers to the Old Filename and NewName is the New Filename
The reason why we add the Path, and NewName is because the Rename-Item function accepts a Path string and a NewName string
import-csv ./rename.csv | rename-item
The CSV File
Path,NewName dog.jpg,222-dog.jpg rat.jpg,333-rat.jpg cat.jpg,111-cat.jpg
Pass txt file to a command
The parentheses is needed to execute that portion first, then pass the content to the Test-Connection
Test-Connection -ComputerName (Get-Content .\filenamewithcomputernames.txt)
Get a Count of Item. Like WC in Linux
Get-ChildItem . -name | Measure-Object
Count : 417 Average : Sum : Maximum : Minimum : Property :
Sorting Object in the pipeline
Get-Process | Sort-Object ID,ProcessName -Unique
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
362 24 8276 3180 0.20 26508 1 AcrobatNotificationClient
148 9 2160 11400 0.03 3144 1 Adobe Crash Processor
1305 86 114728 94496 52.77 10032 1 Adobe Desktop Service
584 2532 10192 26700 31.14 25252 1 AdobeCollabSync
522 24 6748 25096 0.31 25200 1 AdobeCollabSync