Get-ChildItem -Path "C:\Path\To\Directory"
New-Item -Path "C:\Path\To\File.txt" -ItemType File
New-Item -Path "C:\Path\To\NewFolder" -ItemType Directory
Copy-Item -Path "C:\Path\To\Source\File.txt" -Destination "C:\Path\To\Destination\"
Move-Item -Path "C:\Path\To\Source\File.txt" -Destination "C:\Path\To\Destination\"
Remove-Item -Path "C:\Path\To\File.txt"
Remove-Item -Path "C:\Path\To\Folder" -Recurse
Get-Item -Path "C:\Path\To\FileOrFolder" | Select-Object *
Get-ChildItem -Path "C:\Path\To\Directory" -Recurse | Measure-Object -Property Length -Sum
Get-ComputerInfo
Get-WmiObject -Class Win32_OperatingSystem
Get-HotFix
Get-WmiObject -Class Win32_Product
Get-WmiObject -Class Win32_Processor
Get-WmiObject -Class Win32_PhysicalMemory
Get-PSDrive -PSProvider FileSystem
Get-Service -Name "ServiceName"
Get-CimInstance -ClassName Win32_StartupCommand | Select-Object Name, Command, User
Get-NetIPAddress
Get-NetAdapter | Where-Object {$_.Status -eq 'Up'}
Test-Connection -ComputerName "google.com"
Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" }
Get-DnsClientServerAddress
Get-NetIPConfiguration
Get-NetRoute
Disable-NetAdapter -Name "Ethernet"
Enable-NetAdapter -Name "Ethernet"
Get-Process
Stop-Process -Name "ProcessName"
Start-Process -FilePath "C:\Path\To\Program.exe"
Get-Process -Id 1234
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
Stop-Process -Id 1234
New-LocalUser "Username" -Password (ConvertTo-SecureString "Password" -AsPlainText -Force) -FullName "Full Name" -Description "User Description"
Add-LocalGroupMember -Group "Administrators" -Member "Username"
Get-LocalUser
Get-LocalGroup
Disable-LocalUser -Name "Username"
Enable-LocalUser -Name "Username"
Get-Service
Start-Service -Name "ServiceName"
Stop-Service -Name "ServiceName"
Restart-Service -Name "ServiceName"
Get-Service -Name "ServiceName"
Set-Service -Name "ServiceName" -StartupType Automatic
Enter-PSSession -ComputerName "RemoteComputerName"
Exit-PSSession
Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock { Get-Process }
Get-ChildItem Env:
[System.Environment]::SetEnvironmentVariable("VariableName", "VariableValue", "User")
# Save this in a file with the extension .ps1
Write-Output "Hello, World!"
.\script.ps1
schtasks /create /tn "TaskName" /tr "C:\Path\To\script.ps1" /sc daily /st 09:00
Start-Process powershell.exe -ArgumentList "Start-Process -Verb RunAs 'C:\Path\To\script.ps1'"
Install-Module PSWindowsUpdate
Get-WindowsUpdate
Install-WindowsUpdate -AcceptAll
Hide-WindowsUpdate -KBArticleID "KB1234567"
[System.Web.Security.Membership]::GeneratePassword(12, 2)
$PSVersionTable.PSVersion
Clear-Host
Get-Process | Export-Csv -Path "C:\Path\To\Output.csv" -NoTypeInformation
Get-Help Get-Process
Update-Help
Get-Module -ListAvailable
Import-Module "ModuleName"
Find-Module -Name "ModuleName"
Install-Module -Name "ModuleName"
Remove-Module -Name "ModuleName"
Export-ModuleMember -Function "FunctionName" -Alias "AliasName"
git clone https://github.com/username/repo.git
git status
git add .
git commit -m "Your commit message"
git push origin main
git pull origin main
git checkout -b "new-branch-name"
git merge "branch-name"
$SecurePassword = Read-Host -AsSecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
$SecureString = ConvertTo-SecureString "yourpassword" -AsPlainText -Force
New-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My"
Import-Certificate -FilePath "C:\Path\To\Certificate.cer" -CertStoreLocation Cert:\LocalMachine\Root
try {
# Code that might throw an exception
$result = Get-Process -Name "NonExistentProcess"
} catch {
Write-Host "Error occurred: $_"
}
$Error
$Error.Clear()
$ErrorActionPreference = "Stop"
$?
Invoke-RestMethod -Uri "https://api.example.com/data" -Method Get
Invoke-RestMethod -Uri "https://api.example.com/data" -Method Post -Body $jsonBody -ContentType "application/json"
$json = '{"name": "John", "age": 30}'
ConvertFrom-Json $json
$object = @{ name = "John"; age = 30 }
$object | ConvertTo-Json
Get-WmiObject -Class Win32_Processor | Select-Object -Property LoadPercentage
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -Property TotalVisibleMemorySize, FreePhysicalMemory
Get-Counter -Counter "\PhysicalDisk(_Total)\% Disk Time"
Get-Counter -Counter "\Network Interface(*)\Bytes Total/sec"
Measure-Command { # Your script block here }
Invoke-Sqlcmd -Query "SELECT * FROM YourTable" -ServerInstance "ServerName" -Database "DatabaseName"
Invoke-Sqlcmd -Query "SELECT * FROM YourTable" -ServerInstance "ServerName" -Database "DatabaseName" | Export-Csv -Path "C:\Path\To\Export.csv" -NoTypeInformation
Backup-SqlDatabase -ServerInstance "ServerName" -Database "DatabaseName" -BackupFile "C:\Path\To\Backup.bak"
Restore-SqlDatabase -ServerInstance "ServerName" -Database "DatabaseName" -BackupFile "C:\Path\To\Backup.bak"
Compress-Archive -Path "C:\Path\To\Files\*" -DestinationPath "C:\Path\To\Archive.zip"
Expand-Archive -Path "C:\Path\To\Archive.zip" -DestinationPath "C:\Path\To\ExtractedFiles"
Import-Module ActiveDirectory
Get-ADUser -Filter *
Get-ADGroup -Filter *
New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "[email protected]" -Path "OU=Users,DC=example,DC=com" -AccountPassword (ConvertTo-SecureString "Password123!" -AsPlainText -Force) -Enabled $true
Remove-ADUser -Identity "jdoe"
Search-ADAccount -LockedOut
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\Path\To\YourScript.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "MyScheduledTask" -Description "Runs a PowerShell script daily at 9 AM"
Unregister-ScheduledTask -TaskName "MyScheduledTask" -Confirm:$false
docker ps
docker start "ContainerNameOrID"
docker stop "ContainerNameOrID"
docker rm "ContainerNameOrID"
docker pull "ImageName"
Enable-PSRemoting -Force
Enter-PSSession -ComputerName "RemoteComputerName"
Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock { Get-Process }
Disable-PSRemoting -Force
Some of the PowerShell commands in this repository have been inspired by or sourced from the excellent article "Top 51 PowerShell Commands That You Should Know" by Stackify. This article provides a comprehensive guide for developers and IT professionals to use PowerShell for various administrative and development tasks.