50 commonly used Powershell commands
Here’s are 50 commonly used PowerShell commands with explanation of the provided PowerShell cmdlets with examples. These commands are organized into categories for better understanding.
Process and Service Management
-
Get-Process
Lists all running processes.# List all running processes Get-Process # Filter processes with a specific name Get-Process -Name "notepad"
-
Get-Service
Lists all services or details about a specific service.# List all services Get-Service # Check the status of a specific service Get-Service -Name "wuauserv"
File System Operations
-
Get-ChildItem
Lists files and folders in a directory.# List all files and directories Get-ChildItem # Recursively list files in a directory Get-ChildItem -Path "C:\MyFolder" -Recurse
-
Get-Content
Reads the content of a file.# Read the content of a text file Get-Content -Path "C:\example.txt"
-
Set-Content
Writes or replaces content in a file.# Write content to a text file Set-Content -Path "C:\example.txt" -Value "Hello, World!"
-
New-Item
Creates a new file or directory.# Create a new directory New-Item -Path "C:\NewFolder" -ItemType Directory # Create a new file New-Item -Path "C:\example.txt" -ItemType File
-
Remove-Item
Deletes files or directories.# Delete a file Remove-Item -Path "C:\example.txt" # Delete a folder and its contents Remove-Item -Path "C:\NewFolder" -Recurse
-
Rename-Item
Renames a file or directory.# Rename a file Rename-Item -Path "C:\example.txt" -NewName "new_example.txt"
-
Copy-Item
Copies files or directories.# Copy a file Copy-Item -Path "C:\example.txt" -Destination "D:\Backup\example.txt"
-
Move-Item
Moves files or directories.# Move a file Move-Item -Path "C:\example.txt" -Destination "D:\example.txt"
-
Test-Path
Checks if a path exists.# Check if a file exists Test-Path -Path "C:\example.txt"
Filtering and Iteration
-
Select-Object
Selects specific properties from objects.# Select specific properties of processes Get-Process | Select-Object Name, CPU
-
Where-Object
Filters objects based on conditions.# Get services that are running Get-Service | Where-Object {$_.Status -eq "Running"}
-
ForEach-Object
Performs an action for each object in a pipeline.# Stop all services that start with "A" Get-Service | Where-Object {$_.Name -like "A*"} | ForEach-Object {Stop-Service -Name $_.Name}
-
If-Else
Conditional execution of code.# Check if a file exists if (Test-Path -Path "C:\example.txt") { Write-Host "File exists" } else { Write-Host "File does not exist" }
-
Switch
Multi-condition evaluation.# Respond based on input $value = "B" switch ($value) { "A" { Write-Host "Value is A" } "B" { Write-Host "Value is B" } default { Write-Host "Unknown value" } }
Help and Object Analysis
-
Get-Help
Displays help for a command.# Get help for Get-Process Get-Help Get-Process
-
Get-Member
Displays the properties and methods of an object.# Get members of a process object Get-Process | Get-Member
-
Get-Command
Lists all available commands or details about a specific command.# List all commands Get-Command # Get details of a specific command Get-Command Get-Process
Data and File Manipulation
-
Measure-Object
Calculates properties of objects, like count or sum.# Count files in a directory Get-ChildItem -Path "C:\MyFolder" | Measure-Object
-
Out-File
Sends output to a file.# Save output to a file Get-Process | Out-File -FilePath "C:\processes.txt"
-
Export-Csv
Exports objects to a CSV file.# Export processes to a CSV file Get-Process | Export-Csv -Path "C:\processes.csv" -NoTypeInformation
-
Import-Csv
Imports data from a CSV file.# Import data from a CSV file $data = Import-Csv -Path "C:\processes.csv"
-
ConvertTo-Csv
Converts objects to CSV format (does not save to a file).# Convert data to CSV format Get-Process | ConvertTo-Csv
Working with Properties
-
Get-ItemProperty
Gets properties of an item (like registry keys).# Get registry key properties Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion"
-
Set-ItemProperty
Sets properties of an item.# Modify a registry value Set-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "MyValue" -Value "123"
-
New-ItemProperty
Creates a new property for an item.# Add a registry key property New-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "NewValue" -Value "456" -PropertyType String
-
Remove-ItemProperty
Removes a property from an item.# Delete a registry key property Remove-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "MyValue"
Navigation and Environment
-
Get-Location
Gets the current directory.Get-Location
-
Set-Location
Changes the current directory.Set-Location -Path "C:\MyFolder"
-
Clear-Host
Clears the PowerShell console.Clear-Host
Aliases
-
Get-Alias
Lists all aliases or a specific alias.Get-Alias # Get alias for Get-Process Get-Alias -Name "gps"
-
New-Alias
Creates a new alias.New-Alias -Name "list" -Value Get-ChildItem
-
Remove-Alias
Removes an alias.Remove-Alias -Name "list"
Variables
-
Get-Variable
Lists all variables or a specific variable.Get-Variable # Get a specific variable Get-Variable -Name "Path"
-
Set-Variable
Creates or modifies a variable.Set-Variable -Name "MyVar" -Value "Hello, World!"
-
New-Variable
Creates a new variable.New-Variable -Name "NewVar" -Value 123
-
**`Remove-
Variable`**
Removes a variable. powershell Remove-Variable -Name "NewVar"
Remote and Event Management
-
Invoke-Command
Executes commands on remote machines.Invoke-Command -ComputerName "Server01" -ScriptBlock { Get-Process }
-
Enter-PSSession
Starts a remote session.Enter-PSSession -ComputerName "Server01"
-
Exit-PSSession
Ends the remote session.Exit-PSSession
-
Get-EventLog
Retrieves event log entries.Get-EventLog -LogName "Application"
-
Clear-EventLog
Clears event logs.Clear-EventLog -LogName "Application"
Output and Logging
-
Write-Output
Sends data to the pipeline.Write-Output "Hello, World!"
-
Write-Host
Displays text in the console.Write-Host "Hello, World!" -ForegroundColor Green
-
Write-Error
Generates an error message.Write-Error "This is an error message."
-
Write-Warning
Generates a warning message.Write-Warning "This is a warning message."
-
Write-Verbose
Displays detailed information.Write-Verbose "Verbose output" -Verbose
-
Write-Debug
Displays debugging information.Write-Debug "Debugging information" -Debug
-
Out-GridView
Displays data in a grid view.Get-Process | Out-GridView
Conclusion
PowerShell cmdlets provide a versatile and powerful way to manage processes, services, files, directories, system settings, and even remote systems. With simple syntax and robust capabilities, these commands are essential for administrators and developers to automate tasks and streamline workflows.
By understanding cmdlets like
Get-Process
,Get-Service
,Copy-Item
, andWhere-Object
, you can easily handle complex operations, filter and analyze data, and interact with the file system. Advanced cmdlets likeInvoke-Command
,Export-Csv
, andOut-GridView
further extend PowerShell’s functionality, enabling remote execution, data manipulation, and enhanced reporting.Whether you’re a beginner learning the basics or a professional managing intricate infrastructures, mastering these cmdlets equips you with the tools needed to efficiently handle day-to-day tasks and automate repetitive processes.