PowerShell is a scripting language and command-line shell developed by Microsoft, designed for system administration and automation across Windows, Linux, and macOS. It provides access to system functions and a range of commands for automating tasks and managing configurations.
Below are the 30 most common PowerShell commands aimed at enhancing system administrators’ productivity.
We also included a PowerShell commands cheat sheet for quick reference so you can easily find and apply the commands discussed.
Key Takeaways
- PowerShell is a cross-platform system administration and automation tool compatible with Windows, Linux, and macOS.
- We highlight 30 basic PowerShell commands covering file system navigation, file manipulation, system administration, user and permissions management, networking, process management, and working with objects, along with a cheat sheet for quick reference.
- You can automate complex operations through PowerShell scripting and extend its functionality with modules, which provide advanced commands and functions for specialized tasks.
- Best practices include using clear naming conventions, simplifying commands, modularizing code, consistent formatting, avoiding hard-coded values, implementing error handling, and the importance of commenting and documenting your scripts.
Basic Concepts of PowerShell
Cmdlets
Cmdlets are small commands in PowerShell designed to perform a single operation. For example, Get-Content reads content from a file, while Set-Content writes content to a file. Cmdlets can be combined, and their output can be manipulated to handle complex tasks. They form the foundation of PowerShell’s functionality.
Scripts
PowerShell scripts are text files that contain cmdlets and other PowerShell commands saved with a .ps1 extension. They automate longer sequences of tasks and can be simple or complex.
Pipeline
PowerShell Pipeline connects multiple cmdlets, passing the output of one as the input to the next. This chaining of commands enables you to perform complex operations with minimal code. For example, you can list all files in a directory and filter them by size or type in a single line of code.
PowerShell Integrated Scripting Environment (ISE)
ISE is a graphical user interface (GUI) for PowerShell that provides an environment for writing, testing, and debugging scripts. It has syntax coloring, tab completion, and integrated help, along with other neat tools, making it easier to develop and refine PowerShell commands and scripts.
30 Essential PowerShell Commands You Should Know
There are so many Windows PowerShell commands available that it can easily become a bit overwhelming to figure out where to start.
We’ve compiled a comprehensive PowerShell commands list with some of the essential commands you should know for tasks ranging from navigating the file system and file manipulation to system admin and networking.
Navigating the File System
- Get-ChildItem: Lists items in a directory.
- Set-Location: Changes the current directory.
- Push-Location: Saves the current directory on a stack so you can return to it.
- Pop-Location: Returns to the directory saved by Push-Location.
File Manipulation
- New-Item: Creates a new file or directory.
- Remove-Item: Deletes a file or directory.
- Copy-Item: Copies a file or directory to another location.
- Move-Item: Moves a file or directory to a new location.
- Rename-Item: Renames a file or directory.
System Administration
- Get-Service: Lists all services on a computer.
- Start-Service: Starts a stopped service.
- Stop-Service: Stops a running service.
- Restart-Service: Restarts a service.
User & Permissions Management
- Get-LocalUser: Retrieves local user accounts.
- New-LocalUser: Creates a new local user account.
- Remove-LocalUser: Deletes a local user account.
- Get-Acl: Gets access control list (ACL) for a file or resource.
- Set-Acl: Sets the ACL for a file or resource.
Networking Commands
- Test-Connection: Sends ICMP echo requests to a target host to test connectivity.
- Get-NetIPAddress: Retrieves IP address configuration.
- Get-NetAdapter: Lists network adapters.
- Resolve-DnsName: Resolves a DNS name to an IP address.
Process Management
- Get-Process: Lists currently running processes.
- Start-Process: Starts a new process.
- Stop-Process: Stops a running process.
- Wait-Process: Waits for a process to exit.
Working With Objects
- Select-Object: Selects specific properties of an object.
- Where-Object: Filters objects based on property values.
- Sort-Object: Sorts objects by property values.
- Group-Object: Groups objects by property values.
PowerShell Commands Cheat Sheet
We’ve listed the most common PowerShell Commands you should know, but what about an alias for a given command? How about its syntax?
With this cheat sheet, you can get everything you need to know about those essential commands, along with an example, for quick reference.
Alias | Command | Syntax | Example | Description |
Navigating the File System | ||||
Gci, ls, dir | Get-ChildItem | Get-ChildItem -Path <Path> | Get-ChildItem -Path C:\Users | Lists items in a specified directory |
Cd, chdir | Set-Location | Set-Location -Path <Path> | Set-Location -Path C:\Windows | Changes the current directory |
pushd | Push-Location | Push-Location -Path <Path> | Push-Location -Path C:\Windows | Saves the current directory on a stack |
popd | Pop-Location | Pop-Location | Pop-Location | Returns to the directory saved by Push-Location |
File Manipulation | ||||
ni | New-Item | New-Item -Path <Path> -ItemType File | New-Item -Path .\file.txt -ItemType File | Creates a new file |
del, erase, rd, ri, rm, rmdir, rp | Remove-Item | Remove-Item -Path <Path> | Remove-Item -Path .\file.txt | Deletes a file or directory |
copy, cp, cpi, cpp | Copy-Item | Copy-Item -Path <Path> -Destination <Destination> | Copy-Item -Path .\file.txt -Destination .\backup\file.txt | Copies a file to a new location |
mi, move, mp, mv | Move-Item | Move-Item -Path <Path> -Destination <Destination> | Move-Item -Path .\file.txt -Destination .\new\file.txt | Moves a file to a new location |
ren, rni, | Rename-Item | Rename-Item -Path <Path> -NewName <NewName> | Rename-Item -Path .\file.txt -NewName new_file.txt | Renames a file |
System Administration | ||||
gsv | Get-Service | Get-Service | Get-Service | Where-Object {$_.Status -eq “Running”}` |
sasv | Start-Service | Start-Service -Name <Name> | Start-Service -Name “bits” | Starts a stopped service |
spsv | Stop-Service | Stop-Service -Name <Name> | Stop-Service -Name “bits” | Stops a running service |
– | Restart-Service | Restart-Service -Name <Name> | Restart-Service -Name “bits” | Restarts a service |
User & Permissions Management | ||||
glu | Get-LocalUser | Get-LocalUser | Get-LocalUser | Select-Object Name` |
nlu | New-LocalUser | New-LocalUser -Name <Name> | New-LocalUser -Name “User1” | Creates a new local user account |
rlu | Remove-LocalUser | Remove-LocalUser -Name <Name> | Remove-LocalUser -Name “User1” | Deletes a local user account |
– | Get-Acl | Get-Acl -Path <Path> | Get-Acl -Path .\file.txt | Gets ACL for a file or resource |
– | Set-Acl | Set-Acl -Path <Path> -AclObject <AclObject> | Set-Acl -Path .\file.txt -AclObject $acl | Sets the ACL for a file or resource |
Networking Commands | ||||
– | Test-Connection | Test-Connection -ComputerName <Name> | Test-Connection -ComputerName google.com | Tests network connectivity |
– | Get-NetIPAddress | Get-NetIPAddress | Get-NetIPAddress -InterfaceAlias “Ethernet” | Retrieves IP address configuration |
– | Get-NetAdapter | Get-NetAdapter | Get-NetAdapter -Name “Ethernet” | Lists network adapters |
– | Resolve-DnsName | Resolve-DnsName -Name <Name> | Resolve-DnsName -Name google.com | Resolves a DNS name to an IP address |
Process Management | ||||
gps, ps | Get-Process | Get-Process | Get-Process | Where-Object {$_.CPU -gt 100}` |
start, saps | Start-Process | Start-Process -FilePath <FilePath> | Start-Process -FilePath “notepad.exe” | Starts a new process |
spps, kill | Stop-Process | Stop-Process -Name <Name> | Stop-Process -Name “notepad” | Stops a running process |
– | Wait-Process | Wait-Process -Name <Name> | Wait-Process -Name “notepad” | Waits for a process to exit |
Working With Objects | ||||
select | Select-Object | Select-Object -Property <Property> | Get-Process | Select-Object Name, CPU | Selects specific properties of an object |
– | Where-Object | Where-Object {$_.Property -Condition <Value>} | Get-Process | Where-Object {$_.CPU -gt 100} | Filters objects based on property values |
sort | Sort-Object | Sort-Object -Property <Property> | Get-Process | Sort-Object CPU | Sorts objects by property values |
group | Group-Object | Group-Object -Property <Property> | Get-Process | Group-Object ProcessName | Groups objects by property values |
Advanced PowerShell Commands & Techniques
PowerShell scripting extends the functionality of the command line, allowing for more complex and powerful operations. Here are some of the key elements.
- Control Structures: These are the if, else, and switch statements for decision-making and loops (for, for each, while, do-while) for repetitive tasks.
- Functions: Functions are reusable blocks of code that you can call with a name. They help organize scripts into manageable sections and can accept parameters for dynamic operation.
- Error Handling: PowerShell uses try, catch, and finally blocks for error handling. This allows scripts to gracefully handle errors and continue executing or properly terminate.
Scripts can automate almost any task that can be performed in the PowerShell command line. Complex scripts can manage system updates, deploy software across networks, automate file management, and a whole lot more.
The key is combining cmdlets, control structures, and custom functions to create a sequence of operations that run without manual intervention.
Modules expand PowerShell’s capabilities by adding new commands, including cmdlets and functions. After importing a module with Import-Module, you can access additional functionalities for specific tasks, like AzureRM for Azure resources, ActiveDirectory for directory services, and PSDsc for configuration management.
Modules can be either built-in, downloaded from the PowerShell Gallery, or custom-made.
PowerShell Scripting Best Practices
When writing PowerShell scripts, following best practices will keep your code clean, readable, and usable. Here are some general guidelines you should follow.
- Use Clear Naming Conventions: Choose descriptive names for functions and variables. This makes your script self-documenting and easier to understand.
- Keep It Simple: Write simple and straightforward commands. Complex one-liners can be impressive but difficult to read and maintain.
- Modularize Your Code: Break your script into functions. This approach makes your code reusable and easier to test.
- Use Consistent Formatting: Use consistent indentation, spacing, and bracket placement. This consistency improves readability and maintenance.
- Avoid Hard-Coding Values: Use parameters and configuration files instead of hard-coded values. This makes your scripts more flexible and easier to update.
- Implement Error Handling: Use try, catch, and finally, blocks to handle errors. Proper error handling prevents your script from failing silently and makes debugging easier.
- Comment Your Code: Comments are very important for explaining the purpose of your code, how it works, and why specific decisions were made. Comments are valuable for anyone who may work on your script in the future, including your future self.
- Document Your Script: Alongside in-line comments, document at the beginning of your script its purpose, usage, parameters, and any dependencies. It is important for others to understand and use your script.
The Bottom Line
System administrators and IT professionals who want to improve workflow and automate system management would definitely benefit from mastering PowerShell commands.
The PowerShell commands cheat sheet provided is a good starting point, but it’s important to practice these commands and explore beyond the basics.
Advanced scripting, using modules, and learning new commands will unlock PowerShell’s full capabilities.
FAQs
What are commands for PowerShell?
How do I script a PowerShell command?
How do I use PowerShell from the command prompt?
References
- azurerm (Terraform)
- PSDsc (Inedo)
- Getting Started with the PowerShell Gallery (Microsoft)