In This Article
Working with Objects
Get-Process | Get-Member # View the list of members of objects
Get-Service | Where-Object {$_.Status -eq "Running"} # Filter objects by property
Get-Process | Sort-Object CPU -Descending # Sort objects
Files and Directories
Get-ChildItem # List files and directories
Set-Location C: # Change current directory
Copy-Item file.txt D: # Copy a file
Remove-Item file.txt # Delete a file
New-Item newfile.txt # Create a new file
Variables and Data Types
$var = "Hello" # Create a variable
[int]$num = 5 # Integer
[string]$text = "PowerShell" # String
$array = 1,2,3 # Array
$hash = @{key="value"} # Hash table
Pipelines and Filters
Get-Process | Where-Object {$_.CPU -gt 100} # Filter processes by CPU usage
Get-Service | Select-Object Name, Status # Select specific properties
Loops and Conditionals
if ($x -gt 10) { "Greater than 10" } else { "Less or equal" }
for ($i=1; $i -le 5; $i++) { Write-Output $i }
foreach ($item in $array) { Write-Output $item }
while ($count -lt 5) { $count++ }
Functions and Scripts
function Get-Square($num) {
return $num * $num
}
Get-Square 5
Modules and Imports
Get-Module -ListAvailable # List available modules
Import-Module ModuleName # Import a module