Powershell

30 PowerShell Commands Examples

PowerShell is a text-based scripting tool developed by Microsoft on .NET Common Language Runtime (CLR). It was designed for sysadmins and IT professionals so they can manage their work swiftly. It is more powerful than Command Prompt (CMD), because of its extensive capability to create custom scripts and cmdlets for task automation. PowerShell helps in checking the status of services and processes on your computer and creates automated tasks. It can be embedded into other programs.

PowerShell has two modes, one is console and the other is ISE. The PowerShell console helps in managing and manipulating the system by executing built-in commands. While, ISE (Integrated Scripting Environment), helps in writing, testing, and debugging the scripts. Custom scripts are created to install programs, clear the system cache, create or remove users, or launch programs. For instance, PowerShell scripts can help export the titles of 500 videos into text within 10 seconds.

Quick Outline:

30 PowerShell Commands Examples

Conclusion

30 PowerShell Commands Examples

PowerShell commands are the units of scripting language. They help the system admins perform the recurring tasks. To be good at PowerShell, learn its commands and understand its syntax, how it works, and its aliases. Learning PowerShell commands can make you a pro in it.

Let’s have a look at the 30 basic PowerShell commands that are used on a routine by the users with examples.

1. Get-Help

The Get-Help command gets help regarding PowerShell commands. A command is placed alongside the Get-Help command to seek help. It displays the name, syntax, alias, and remarks of the specified command.

Example:

This example will get the helping material of the desired command using the Get-Help command:

Get-Help Get-Process

 

First, we placed the Get-Help command. Then placed the command to get helping material related to that command:

2. Get-Process

The Get-Process command gets the list of processes on Windows. It displays the process name, id, and CPU usage. It gets all the processes on the computer when executed without parameters. However, the individual process can be retrieved by specifying it to the Get-Process command. Users can specify the process id or name of the process to get information about it.

Example:

This example will get a single process information using the Get-Process command:

Get-Process -Name system

 

According to the above command, first, place the Get-Process command and then specify the process name using the -Name parameter:

3. Get-Service

The Get-Service command gets the list of all the services on the system including running, stopped, or suspended. It displays the service status, name, and the DisplayName.

Example:

This example will get the service information by providing its name to the Get-Service command:

Get-Service -Name ALG

 

In the above code, first, the Get-Service command is placed, and then the service name is specified using the -Name parameter:

4. Get-Variable

The Get-Variable command gets the variables in the current PowerShell console. It gets variable names and values. It displays all the variables when they are executed without parameters. However, the specified variables can be retrieved by specifying the name of the variable.

Example:

This example will get a single variable and display its value information in detail using the Get-Variable command:

Get-Variable -Name Host -ValueOnly

 

In the above-mentioned code:

  • First, specify the Get-Variable command.
  • Then specify the name of the variable using the -Name parameter.
  • After that, use the -ValueOnly parameter to get the value of the variable in detail:

5. Get-Command

The Get-Command command gets the list of all the installed PowerShell commands on Windows. It contains the command types including aliases, functions, and cmdlets. It gets the name, CommandType, version, and source of the commands.

Example:

This example will get the list of only PowerShell cmdlets installed on the computer using the Get-Command command:

Get-Command -CommandType cmdlet

 

According to the above code:

First, use the Get-Command command, then specify the command type using the -CommandType parameter:

6. Show-Command

The Show-Command command opens the graphical prompt window which contains the list of installed PowerShell commands on the system. Users can scroll this window to see and know about these commands in detail.

Example:

This example will open the graphical prompt window containing the list of all the commands using the Show-Command command:

Show-Command

 

Example:

This example will open the graphical prompt window for the specified command using the Show-Command command:

Show-Command -Name Invoke-Command

 

In the above-mentioned command:

First, use the Show-Command command then specify the command name using the -Name parameter:

7. Hostname

The Hostname command gets the DNS (Domain Name System) name of the computer. The hostname is the name specified by the connected network.

Example:

This example will get the hostname of the computer using the Hostname command:

Hostname

 

8. Get-Clipboard

The Get-Clipboard command gets the content copied to the clipboard as text in the PowerShell console. If the content is of multiple lines then it gets the content as an array of strings.

Example:

This example will get the content copied to the clipboard using the Get-Clipboard command:

Get-Clipboard

 

9. Get-Date

The Get-Date command gets the date and time of the computer in the PowerShell console. Various parameters can be with it to get the customized date and time.

Example:

This example will get the date and time on the computer using PowerShell’s Get-Date command:

Get-Date

 

10. Get-ExecutionPolicy

The Get-ExecutionPolicy command gets the execution policies in the PowerShell console. Execution policies are the security policies that determine whether you can load and run the specific script or not on Windows. These execution policies are set by the PowerShell’s Set-ExecutionPolicy command.

Example:

This example will get the current execution policy on Windows using the Get-ExecutionPolicy command:

Get-ExecutionPolicy

 

To learn more about the execution policies read this article.

11. Get-History

The Get-History command gets the history of the commands that were executed on the PowerShell console recently in the current session. It is very useful when you want to know what you executed in the current session.

Example:

This example will get the commands list that were executed in the current session using the Get-History command:

Get-History

 

12. Ping

The Ping command tests whether the specified server is accessible or not. To do so, it sends the data packets to that server. If it receives the data packets in return then that server can be contacted. It displays the time consumed during the sending and receiving of the data packets.

Example:

This example will ping the specified web server and check its availability using the Ping command:

ping www.linuxhint.com

 

13. Test-Connection

The Test-Connection command provides the basic information about the computer network such as the computer name, or IP address. It pings the computer network just like the Ping command. It first sends the ICMP echo request and waits for its return. If it returns then that computer can be contacted, else it can be contacted.

Example:

This example will test the network connection of the provided address using the Test-NetConnection command:

Test-Connection www.linuxhint.com

 

14. Get-NetIPAddress

The Get-NetIPAddress command retrieves the IP address information of your computer. It is a network management command that gets the details of both IPv4 and IPv6 protocols. It gets the information of both individual and multiple IP addresses.

Example:

This example will get the IP address information in detail using the Get-NetIPAddress command:

Get-NetIPAddress

 

15. Clear-History

The Clear-History command deletes the history of commands executed in the current session. Its alias is clhy. Executing just its alias will also clear the current session’s history.

Example:

This example will clear the history of the executed commands in the current PowerShell session using the Clear-History command:

Clear-History

 

16. Clear-Host

The Clear-Host command clears all the text including commands and text displayed on the screen. However, it does not clear the history of executed commands. Its standard alias is the cls.

Example:

This example will clear the text including output commands using the Clear-Host command:

Clear-Host

 

17. Clear-Content

The Clear-Content command clears the content from the specified file but does not remove the file. Users need to specify the file to the Clear-Content command to clear its content. Its standard alias is clc. If its standard alias is executed then it does the same job as the Clear-Content command.

Example:

This example will clear the content of the specified file using the Clear-Content command:

Clear-Content -Path C:\Documents\New.txt

 

According to the above code:

First, use the Clear-Content command and then specify the file path using the -Path parameter:

18. Get-ChildItem

The Get-ChildItem command gets the items and child items from specified locations. A child item is one that is located inside a sub-folder.

Example:

This example will get the child items from the specified location using the Get-ChildItem command:

Get-ChildItem C:\Documents

 

19. Get-Content

The Get-Content command gets the content of a specified text file by the user. It reads the content line by line.

Example:

This example will get the content of a text file and print it into the PowerShell console using the Get-Content command:

Get-Content -Path C:\Documents\New.txt

 

According to the above code:

First, use the Get-Content command and then specify the text file path using the -Path parameter:

20. Get-Item

The Get-Item command gets the items from the specified location. But, it doesn’t get the item’s content like the Get-Content command does.

Example:

This example gets all the items in the given locations using the Get-Item command:

Get-Item C:\*

 

21. Copy-Item

The Copy-Item command copies the content from one location to another. It has the capability to copy the content from one computer to another.

Example:

This example will copy a text file using the Copy-Item command:

copy-item -Path C:\Documents\New.txt -Destination C:\Documents\Output

 

In the above-mentioned code:

  • First use the Copy-Item command.
  • Then, place the path of the file you want to copy using -Path parameter.
  • After that, use the -Destination parameter and specify the destination path where the file will be copied:

22. Export-CSV

The Export-CSV command converts or exports the objects into CSV string values and saves them into the CSV file. Its core function is to export the PowerShell console data into a CSV file. In case, there does not exist a CSV file then it creates it.

Example:

This example will create and export the data into a CSV file using the Export-CSV command:

Get-Process | Export-CSV -Path C:/Documents/New.CSV

 

In accordance with the above-mentioned code:

  • First, place the code or data to be exported into a CSV file, such as a Get-Service command.
  • Then, pipe it to the Export-CSV command.
  • After that use the -Path parameter and specify the CSV file path where the data will be exported:

23. Import-CSV

The Import-CSV command imports the content from a CSV file in a table-familiar format. It imports the CSV file data within the PowerShell console.

Example:

This example will import the content of a CSV file within the PowerShell console using Import-CSV command:

Import-CSV -Path C:\Documents\New.CSV

 

In accordance with the above code, to import the content of a CSV file, first, use the Import-CSV command and then specify the CSV file path using the -Path parameter:

24. Remove-Item

The Remove-Item command deletes the item from the location specified by the user. It deletes any item in Windows such as files, functions, or folders. Its standard alias is rm, which does the same job when used instead of the Remove-Item command.

Example:

This example will delete the text file from the specified location using the Remove-Item command:

Remove-Item -Path C:\Documents\New.CSV

 

To remove the file, first use the Remove-Item command then specify the file path using the -Path parameter:

25. Rename-Item

The Rename-Item command renames the item specified by the user. It changes the name of the file without affecting the data inside the file. It has the capability of renaming one or more files at once.

Example:

This example will rename the text file using the Rename-Item command:

Rename-Item -Path C:\Documents\New.txt -NewName NewName.txt

 

According to the mentioned code:

  • First, place the Rename-Item command.
  • Then, assign the file you want to rename using the -Path parameter.
  • After that, use the -NewName parameter and assign the new name to the file:

26. Test-Path

The Test-Path command checks the availability of the specified path in Windows. If the specified exists then PowerShell will return True, if it does not exist then it will return False. It has the ability to check the file, folder, or registry paths.

Example:

This example will test if the specified path exists or not using the Test-Path command:

Test-Path C:\Documents\NewName.txt

 

To test the path, first, use the Test-Path command, then specify the file path:

27. Write-Host

The Write-Host command processes the data onto the screen or simply writes and displays it on the screen. Unlike the Echo and Write-Output command has the ability to customize the text, such as coloring the output text.

Example:

This example will change the text color and background color in PowerShell using the Write-Host command:

Write-Host "Hello World" -foregroundcolor white -backgroundcolor red

 

In accordance with the above code:

  • First, use the Write-Host command to enter the text.
  • Then, use the -foregroundcolor parameter and assign the color.
  • After that, use the -backgroundcolor parameter and assign the color:

28. Write-Output

The Write-Output command writes the specified objects or text to the pipeline. It can output the content specified to the variable, by just specifying the variable name with it. It outputs the content whatever is specified to it. If there is no pipeline alongside it then it will display the content with the PowerShell console.

Example:

This example will output the text in the console with the help of Write-Output command:

write-output "Hello World"

 

To output the text, first use the Write-Output command and then specify the text:

29. Out-File

The Out-File command exports or saves the output of the PowerShell console into a text file specified by the user. The format of the content will be the same as it was in the console. It is a very useful utility to save the information for later use.

Example:

This demonstration will export the output to the specified text file using the Out-File command:

Get-Service | out-file -filepath C:\Documents\NewFile.txt

 

According to the above code:

  • First, use the Get-Process command, or any desired command. There can also be a text.
  • Then, pipe it to the Out-File cmdlet.
  • After that use the -FilePath parameter and specify the text file to save the content inside it:

30. Format-Table

The Format-Table command formats the output in the table format. Its standard alias is ft. Alongside displaying multiple values in the table format, it can display the single selective properties as well.

Example:

This example will format the output of the given command in the table format using Format-Table command:

Get-Process | Format-Table -wrap

 

In accordance with the above command:

  • First, use the Get-Service command or any other desired command.
  • Then, pipe it to the Format-Table command.
  • Lastly, use the -wrap parameter to avoid truncation:

Conclusion

PowerShell commands are the units of scripting language. It helps the system admins perform the recurring tasks swiftly. Learning PowerShell commands and understanding its basics such as its syntax, how it works, and its aliases can make you a pro in it. It has commands like Get-Help, Get-Process, Get-Service, Get-Date, Ping, Get-Clipboard, Clear-History, or Out-File that are used on a routine basis by a standard PowerShell user. All these mentioned commands and more are explained with the help of examples in the above guide.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.