Get-ChildItem -Path "C:\Path\To\Directory"New-Item -Path "C:\Path\To\File.txt" -ItemType FileNew-Item -Path "C:\Path\To\NewFolder" -ItemType DirectoryCopy-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" -RecurseGet-Item -Path "C:\Path\To\FileOrFolder" | Select-Object *Get-ChildItem -Path "C:\Path\To\Directory" -Recurse | Measure-Object -Property Length -SumGet-ComputerInfoGet-WmiObject -Class Win32_OperatingSystemGet-HotFixGet-WmiObject -Class Win32_ProductGet-WmiObject -Class Win32_ProcessorGet-WmiObject -Class Win32_PhysicalMemoryGet-PSDrive -PSProvider FileSystemGet-Service -Name "ServiceName"Get-CimInstance -ClassName Win32_StartupCommand | Select-Object Name, Command, UserGet-NetIPAddressGet-NetAdapter | Where-Object {$_.Status -eq 'Up'}Test-Connection -ComputerName "google.com"Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" }Get-DnsClientServerAddressGet-NetIPConfigurationGet-NetRouteDisable-NetAdapter -Name "Ethernet"Enable-NetAdapter -Name "Ethernet"Get-ProcessStop-Process -Name "ProcessName"Start-Process -FilePath "C:\Path\To\Program.exe"Get-Process -Id 1234Get-Process | Sort-Object CPU -Descending | Select-Object -First 5Stop-Process -Id 1234New-LocalUser "Username" -Password (ConvertTo-SecureString "Password" -AsPlainText -Force) -FullName "Full Name" -Description "User Description"Add-LocalGroupMember -Group "Administrators" -Member "Username"Get-LocalUserGet-LocalGroupDisable-LocalUser -Name "Username"Enable-LocalUser -Name "Username"Get-ServiceStart-Service -Name "ServiceName"Stop-Service -Name "ServiceName"Restart-Service -Name "ServiceName"Get-Service -Name "ServiceName"Set-Service -Name "ServiceName" -StartupType AutomaticEnter-PSSession -ComputerName "RemoteComputerName"Exit-PSSessionInvoke-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.ps1schtasks /create /tn "TaskName" /tr "C:\Path\To\script.ps1" /sc daily /st 09:00Start-Process powershell.exe -ArgumentList "Start-Process -Verb RunAs 'C:\Path\To\script.ps1'"Install-Module PSWindowsUpdate
Get-WindowsUpdateInstall-WindowsUpdate -AcceptAllHide-WindowsUpdate -KBArticleID "KB1234567"[System.Web.Security.Membership]::GeneratePassword(12, 2)$PSVersionTable.PSVersionClear-HostGet-Process | Export-Csv -Path "C:\Path\To\Output.csv" -NoTypeInformationGet-Help Get-ProcessUpdate-HelpGet-Module -ListAvailableImport-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.gitgit statusgit add .git commit -m "Your commit message"git push origin maingit pull origin maingit 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 -ForceNew-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My"Import-Certificate -FilePath "C:\Path\To\Certificate.cer" -CertStoreLocation Cert:\LocalMachine\Roottry {
# 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 GetInvoke-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-JsonGet-WmiObject -Class Win32_Processor | Select-Object -Property LoadPercentageGet-WmiObject -Class Win32_OperatingSystem | Select-Object -Property TotalVisibleMemorySize, FreePhysicalMemoryGet-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" -NoTypeInformationBackup-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 ActiveDirectoryGet-ADUser -Filter *Get-ADGroup -Filter *New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "jdoe@example.com" -Path "OU=Users,DC=example,DC=com" -AccountPassword (ConvertTo-SecureString "Password123!" -AsPlainText -Force) -Enabled $trueRemove-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:$falsedocker psdocker start "ContainerNameOrID"docker stop "ContainerNameOrID"docker rm "ContainerNameOrID"docker pull "ImageName"Enable-PSRemoting -ForceEnter-PSSession -ComputerName "RemoteComputerName"Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock { Get-Process }Disable-PSRemoting -ForceSome 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.