In This Article
Remote Management
Enter-PSSession -ComputerName Server01 # Start a remote session
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } # Run a command remotely
Error Handling
try {
Get-Item "nonexistent.txt"
} catch {
Write-Output "Error: $_"
}
Regular Expressions
"Hello123" -match "\d+" # Returns True if string contains digits
"abc123" -replace "\d+", "" # Remove digits
Working with JSON and XML
$json = Get-Content file.json | ConvertFrom-Json
$json.PropertyName
$json | ConvertTo-Json | Set-Content new.json
[xml]$xml = Get-Content file.xml
$xml.DocumentElement.ElementName
Jobs and Background Tasks
Start-Job -ScriptBlock { Get-Process } # Run in background
Get-Job # Show jobs
Receive-Job -Id 1 # Get job result
Remove-Job -Id 1 # Remove job
Scheduled Tasks
Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute "notepad.exe") `
-Trigger (New-ScheduledTaskTrigger -At 12pm -Daily) -TaskName "NotepadTask"
Security and Credentials
Get-Credential # Prompt for credentials
Invoke-Command -ComputerName Server01 -Credential (Get-Credential) -ScriptBlock { Get-Service }
Performance and Logging
Measure-Command { Get-Process } # Measure execution time
Start-Transcript -Path log.txt # Start logging session
Stop-Transcript # Stop logging