PowerShell cmdlets, are commands in PowerShell that perform specific tasks. Here is a list of some commonly used PowerShell cmdlets and how to use them:
- Get-Process – Displays information about running processes on the system. Example:
Get-Process
- Get-Service – Displays information about running services on the system. Example:
Get-Service
- Get-HotFix – Displays information about installed hotfixes on the system. Example:
Get-HotFix
- Get-ChildItem – Displays a list of files and directories in a specified location. Example:
Get-ChildItem C:\
- Get-Content – Displays the contents of a file. Example:
Get-Content C:\example.txt
- Set-Content – Replaces the contents of a file with new content. Example:
Set-Content C:\example.txt "New Content"
- New-Item – Creates a new file or directory. Example:
New-Item C:\example.txt
- Remove-Item – Deletes a file or directory. Example:
Remove-Item C:\example.txt
- Rename-Item – Renames a file or directory. Example:
Rename-Item C:\example.txt NewName.txt
- Move-Item – Moves a file or directory to a new location. Example:
Move-Item C:\example.txt D:\
- Copy-Item – Copies a file or directory to a new location. Example:
Copy-Item C:\example.txt D:\
- Test-Path – Checks if a file or directory exists. Example:
Test-Path C:\example.txt
- Get-ItemProperty – Displays the properties of a file or directory. Example:
Get-ItemProperty C:\example.txt
- Get-Date – Displays the current date and time. Example:
Get-Date
- Get-Random – Generates a random number. Example:
Get-Random
- Write-Host – Writes text to the console. Example:
Write-Host "Hello, World!"
- Read-Host – Prompts the user for input. Example:
Read-Host "What is your name?"
- Out-File – Sends output to a file. Example:
Get-Process | Out-File C:\example.txt
- Format-List – Displays output in a formatted list. Example:
Get-Process | Format-List Name, Id, CPU
- Format-Table – Displays output in a formatted table. Example:
Get-Process | Format-Table Name, Id, CPU
- Sort-Object – Sorts output by a specified property. Example:
Get-Process | Sort-Object CPU -Descending
- Select-Object – Selects specific properties to display. Example:
Get-Process | Select-Object Name, Id, CPU
- Where-Object – Filters output based on a specified condition. Example:
Get-Process | Where-Object { $_.CPU -gt 50 }
- ForEach-Object – Performs a specified operation on each item in a collection. Example:
Get-ChildItem | ForEach-Object { Write-Host $_.Name }
- If – Executes a block of code if a condition is true. Example:
if ($a -lt $b) { Write-Host "a is less than b" }
- Else – Specifies a block of code to execute if the preceding If condition is false. Example:
If ($a -gt 10) { Write-Host "a is greater than 10" } Else { Write-Host "a is less than or equal to 10" }
- Switch – Selects a block of code to execute based on a specified value. Example:
switch ($a) { 1 { Write-Host "a is 1" }; 2 { Write-Host "a is 2" }; default { Write-Host "a is something else" } }
- For – Executes a block of code a specified number of times. Example:
for ($i = 1; $i -le 10; $i++) { Write-Host $i }
- While – Executes a block of code as long as a condition is true. Example:
while ($a -lt 10) { $a++ }
- Do-While – Executes a block of code at least once and then continues to execute as long as a condition is true. Example:
do { Write-Host $a; $a++ } while ($a -lt 10)
- Break – Exits a loop. Example:
for ($i = 1; $i -le 10; $i++) { if ($i -eq 5) { Break } Write-Host $i }
- Continue – Skips to the next iteration of a loop. Example:
for ($i = 1; $i -le 10; $i++) { if ($i -eq 5) { Continue } Write-Host $i }
- Try-Catch – Handles errors in a block of code. Example:
try { Get-ChildItem -Path "C:\example.txt" } catch { Write-Host "An error occurred" }
- Test-NetConnection – Checks the status of a network connection. Example:
Test-NetConnection -ComputerName "google.com" -Port 80
- New-NetFirewallRule – Creates a new firewall rule. Example:
New-NetFirewallRule -DisplayName "My Firewall Rule" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 80
- Enable-NetFirewallRule – Enables a firewall rule. Example:
Enable-NetFirewallRule -DisplayName "My Firewall Rule"
- Disable-NetFirewallRule – Disables a firewall rule. Example:
Disable-NetFirewallRule -DisplayName "My Firewall Rule"
- Get-NetAdapter – Displays information about network adapters. Example:
Get-NetAdapter
- Set-NetAdapter – Configures network adapters. Example:
Set-NetAdapter -Name "Ethernet" -Dhcp Enabled
- Get-NetIPAddress – Displays information about IP addresses. Example:
Get-NetIPAddress
- Set-NetIPAddress – Configures IP addresses. Example:
Set-NetIPAddress -InterfaceIndex 1 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
- Get-NetRoute – Displays information about IP routing tables. Example:
Get-NetRoute
- Set-NetRoute – Configures IP routing tables. Example:
Set-NetRoute -DestinationPrefix 10.0.0.0/8 -InterfaceIndex 1 -NextHop 192.168.1.1
- Get-NetTCPConnection – Displays information about TCP connections. Example:
Get-NetTCPConnection
- Stop-Process – Stops a running process. Example:
Stop-Process -Name notepad
This is just a small selection of the many cmdlets available in PowerShell. To learn more about cmdlets and their usage, you can refer to the official Microsoft documentation.