However, mastering the functionality and flexibility of PowerShell involves a steep learning curve. If you are just getting started with PowerShell, here are the essential commands you can learn to master this scripting language in the long run.
1. Get-Help
Get-Help, as the name suggests, is part of PowerShell’s integrated help system. It helps you find necessary information for the command, concepts, and functions, identify alias, scripts, and more.
To get help for a PowerShell cmdlet, you need to use the Get-help cmdlet followed by a cmdlet name. For example, to view the synopsis and syntaxes associated with the get-process cmdlet, type:
This command can read both comment-based and XML-based help provided by the function author.
Alternatively, you can use the Get-Help -online command to get help for a PowerShell cmdlet online. For example, to view Microsoft’s online documentation for the Get-Content cmdlet, type:
2. Get-Process
The Get-Process command helps you retrieve and show a list of all the active system processes with their identifiers (IDs). You can use it as an efficient alternative to Windows Task Manager to view, stop and restart system processes.
For example, if you need to stop the GameBar process, first you need to find the process ID associated with it. So, type:
This command will show all the running system processes. Next, find the ID associated with the process you want to stop. To stop the process, type:
Here -ID 20496 is the ID of the process (GameBar) you want to stop.
3. Start-Process
You can use the Start-Process cmdlet in PowerShell to start one or more processes on a local computer. To use the cmdlet, type Start-Process followed by the process name. For example, if you want to start a new notepad process, type:
Additionally, you can use the parameters of Start-Process to specify options. For example, if you need to launch a process as administrator, type:
4. Get-Command
The Get-Command lets you view all the PowerShell commands installed on your computer. Similar to Get-Help, you can use the Get-Command followed by a search query to find commands for a specific feature.
Since the Get-Command displays all the commands, you can specify parameters to find features with a specific name and CommandType. For example, to find cmdlets (CommandTypes) that start with A (Name), type:
Alternatively, type Get-Help Get-Command -Examples to view more examples.
5. Get-Service
The Get-Service cmdlet lets you view your computer’s status and list of services. By default, the Get-Service command returns all the (stopped and running) services.
You can use the parameters to specify and find services depending on their status, name, and dependent services. For example, to view all the services starting with the name Win, type:
6. Get-ChildItem
You can use PowerShell to search through directories. The Get-ChildItem command is a handy cmdlet to look for folders and files and quickly perform content-based searches without using File Explorer.
To view all the top-level folders in the C:\ directory, type:
Additionally, use the -Path parameter to view a particular folder, sub-folders, and content. For example, to view all the sub-folders and files in the Programs Files folder, type:
Additionally, use the -Recurse parameter to view all the files in the specified folder and the -Name parameter to view item names in a directory.
In the above command, replace sub-folder with the folder name to view its content.
7. Copy-Item
The Copy-Item cmdlet lets you copy-paste files and folders and their contents to a different directory. To copy files and folders, type Copy-Item followed by the source -Path, -Destination parameter, and destination address. For example, to copy E:\Folder1 and its contents to E:\Folder2, type:
Note that the -Recurse parameter in the above command is responsible for moving all the folder contents. Without it, PowerShell will only copy the top-level folder (Folder1) and files specified in the command.
8. Move-Item
Similarly, to move an item, you can use the Move-Item cmdlet. For example, to move the folder, files, sub-folders, and all its contents to your specified destination, type:
9. Remove-Item
The Remove-Item cmdlet lets you delete files, folders, functions, and other data types from the specified directory. For example, to delete the Test.txt file in the E:\Folder1 folder, type:
10. Get-Content
The Get-Content cmdlet lets you view the content of an item item without using a text editor. For example, to retrieve the contents of the Test.txt file, type:
You can further specify the content length to view using the -TotalCount parameter.
11. Clear-Content
You can use the Clear-Content cmdlet to delete the contents of a specified file without deleting the file itself. Useful for task automation where you have a hard-coded file name but want to have a clean file each time the script runs.
To test the command, create a text file with some content in it. Next, type:
This will delete the contents of the file without deleting the file.
12. Set-ExecutionPolicy
The default execution policy in PowerShell is set to Restricted. This prevents the execution of malicious scripts in the PowerShell environment. However, when you execute a local PowerShell script, you may encounter the execution script is disabled on this system error.
The Set-ExecutionPolicy cmdlets let you change the security levels for script execution. To know your current execution policy, type:
If you need to execute an unsigned script, in an elevated PowerShell prompt, type:
Other valid Set-ExecutionPolicy values include Restricted, AllSigned, and Unrestricted.
13. Set-Location
By default, PowerShell uses C:\Users\Username as the default working directory. The Set-Location cmdlet lets you set the current working directory to a specified location. Useful if you want to run a script or command from a specific location without having to specify the path each time.
For example, to set C:\Users\Username\Documents as the current working directory, type:
This is a temporary measure as PowerShell will reset the working directory back to its default directory after the restart.
14. Export-CSV
If you want to export and present PowerShell output in a more organized way, you can use the Export-CSV cmdlet. It takes the output file for the specified command and converts it to a CSV file.
To test the command, try the following command:
The above command will create a psporcess.csv file with all the active processes’ data.
15. ConvertTo-HTML
If you would rather create an HTML report, you can use the ConvertTo-HTML Cmdlet. To create an HTML report for all the running process on your PC, type :
In the above command, psprocess is the name of the export file, and HTML is the extension. You can access the exported HTML file in the current working directory located at C:\Users\username.
16. Get-History
You can use the Up-Down arrow key to scroll through the recently executed commands in PowerShell. However, to view a list of all the recently executed commands in your current session at once, you can use the Get-History cmdlet.
It will display a list of all the recently executed commands with their ID. Useful if you want to view the complete context of the previously executed commands. To do this, type:
For example, to view the execution details such as status, start and end time, and duration for the third command, type:
To rerun any command from the list, type:
For example, type Invoke-History 3 to rerun a previously executed command without typing it again.
Additionally, use Clear-History to clear history for the current session.
Now that you have a basic idea of PowerShell commands, go ahead and explore our guide on best PowerShell Cmdlets to improve your Windows admin skills. Here, you can learn to work with data using cmdlets, format tables and list, and a quick overview of the Get-Member command.
PowerShell Commands to Streamline Your Tasks
PowerShell is known for its automation capabilities. This can help you automate hundreds of activities in your development work to save time and improve productivity.
While we have only covered the basic commands, try to explore the syntax, alias, and variables, functions available on many of these commands to master this highly efficient scripting language.