19 useful Windows PowerShell commands

19 useful Windows PowerShell commands

Windows PowerShell is a new command-line tool and is gradually becoming the future alternative to the Command Prompt. However, learning and using Windows PowerShell is relatively more difficult than CMD is familiar with the user. Here are 20 useful PowerShell commands in this article!

Get-Help [help]

If you are new to PowerShell, you may be in trouble; And in such situations, Get-Help becomes your savior. It provides essential information about PowerShell cmdlets, commands, functions, scripts, and workflows.

Also, it’s easy: You need to type Get-Help followed by the command, in which you search for the details. For example, you can get information about “Get-Process” by using Get-Help Get-Process.

Get-Command [gcm]

Windows PowerShell lets you discover commands and features with Get-Command. It displays the list of commands of a specific feature or for a specific purpose based on your search parameters.

You just need to type Get-Command followed by your search query in PowerShell. For example, Get-Command * -service * displays commands with “-service” in its name. Remember to use the asterisks on both sides of your query as it’s better to search for the unknown.

Invoke-Command [icm]

When you want to run a local or remote PowerShell command or script on one or more of your computer (s) – “Invoke-Command” is your friend. It is very easy to use and helps you control the computer series.

You must enter Invoke-Command followed by a command or script with its full path. For example, you can run the “Get-EventLog” command using Invoke-Command -ScriptBlock {Get-EventLog system -Newest 50} or on the remote “Server01” using Invoke-Command-ScriptBlock {Get-EventLog system-Newest 50} -ComputerName Server01.

Invoke-Expression [iex]

Invoke-Expression runs a command or other expression. If you are supplying an expression or a string as its input, this command will first evaluate it, then run it, but also only operate locally, unlike the previous command.

You must enter Invoke-Expression followed by a command or an expression. For example, you could assign a variable “$ Command” to a string that says “Get-Process”. When you run Invoke-Expression $ Command, “Get-Process” is run as a command on your local computer.

Invoke-WebRequest [iwr]

You can download, sign in and delete information on web pages and web services while working on Windows PowerShell using Invoke-WebRequest.

You must use it as Invoke-WebRequest followed by its parameters. For example, you can get links on a certain website using commands like  (Invoke-WebRequest -Uri “https://docs.microsoft.com”).Links.Href.

Set-ExecutionPolicy

Although creating and executing scripts (with the extension “ps1”) in Windows PowerShell is possible; However, there are restrictions for security purposes. But you can switch security levels by using the Set-ExecutionPolicy command.

You can type Set-ExecutionPolicy followed by one of four security levels – Restricted, Remote Signed, Signed All, or Unrestricted to use the command. For example, you can specify a restricted policy state using Set-ExecutionPolicy -ExecutionPolicy Restricted.

Get-Item [gi]

If you are looking for information about an item at any particular location, say a file on your hard drive, Get-Item is the best way to get it in Windows PowerShell. You must know that it does not receive the contents of the item, such as files and subfolders in a certain directory unless you explicitly specify it.

You must enter a Get-Item followed by a path or string with its parameters if any. For example, you could get all items (files or folders) that start with “M” in the current directory using Get-Item M *. Along with the contents of the directories, it can also retrieve the contents of the registry keys.

Copy-Item [copy]

If you need to copy files and folders on the archive disk or registry entries and keys in the registry, you can use the Copy-Item. It works similar to the “cp” command we have in the Command Prompt, but it’s much better.

You can use the Copy-Item command to copy and rename items in the same order – providing the new name as the target. For example, you can copy and rename “Services.htm” to “MyServices.txt” using the “C:Services.htm” -Destination “C:MyDataMyServices.txt”.

Remove-Item [del]

If you want to delete items such as files, folders, functions and registry keys and variables, Remove-Item is the command for you. The thing I find interesting is that, it provides parameters to include and exclude items.

You can use the Remove-Item command to delete items from specific locations by using parameters. For example, you could delete the “MyServices.txt” file with the Remove-Item “C:MyDataMyServices.txt” command.

Get-Content [cat]

When you need to view the content of a text file in a particular location, you open and read it in a code editor such as Notepad ++. In Windows PowerShell, you can use Get-Content to retrieve content without opening the file.

For example, you can get 50 lines of content of “Services.htm”, then you can use Get-Content ““C:Services.htm” -TotalCount 50.

Set-Content [sc]

You can save text to files using Set-Content, similar to the “echo” command of Bash Shell. Combined with Get-Content, you can also access the contents of a file and copy it to another file using this command.

For example, you can enter Set-Content to write or replace the contents of a new content file. In addition, you can combine it with the example of the previous command to save its output to a new file called “Sample.txt” using Get-Content “C:Services.htm” – TotalCount 50 | Set-Content “Sample.txt”.

Get-Variable [gv]

 

If you are looking to use variables in Windows PowerShell, the Get-Variable command will help you visualize the values ​​of the variables. It shows them in tabular form and allows inclusion, exclusion and use of wildcards.

You can use this command by entering Get-Variable followed by its options and parameters. For example, you can retrieve the value for a variable named “desc” using the following code: Get-Variable -Name “desc”.

Set-Variable [set]

You can assign or change / reset the value of the variable using the Set-Variable command. As a shortcut, you can also set a simple variable using the format $ {$ Name = Value} $, like $ desc = “A Description”.

You can use the Set-Variable command followed by its parameters to set a variable. For example, we can set a value for a variable named “desc” by using the Set-Variable -Name “desc” -Value “A Description” command.

Get-Process [gps]

We often use Task Manager to find processes running on our computer. In Windows PowerShell, anyone can use Get-Process to get a list of running processes, which you can handle more.

You can write commands like Get-Process along with your search query. For example, if you need information about processes that have “discovered” in their name, you can enter Get-Process * explore *.

Start-Process [saps]

Windows PowerShell makes it easy to start one or more processes on your PC. I see this command very handy in scripting applications because it is one of the must have commands that you will need to automate a task.

You can type Start-Process followed by its parameters to use the command. For example, you can start Notepad by typing Start-Process -FilePath “notepad” -Verb runAs in Windows PowerShell.

Get-Service [gsv]

When you need information about specific services (running or stopping) on ​​your computer, you can use the Get service. It displays the services installed in your system and provides options to filter and include and exclude them.

If you want to use this command, you can enter Get-Service followed by its parameters. For example, type the following Get-Service | Where-Object {$ _. Status -eq “Running”} to get services running on your system.

Start-Service [sasv]

If you want to start a service on your computer, the Start-Service command can help you do the same from Windows PowerShell. I find it powerful enough to start a service even if that service is disabled on your computer.

You need to specify the name of the service while using the Start-Service command. For example, Start-Service -Name “WSearch” starts the “Windows Search” service on your local computer.

Stop-Service [spsv]

If you want to stop services running on your computer, the Stop-Service command will be useful. You need to specify the name of the service along with the Stop-Service. For example, enter Stop-Service -Name “WSearch” to stop the “Search Windows” service on your computer.

ConvertTo-HTML

PowerShell can provide great information about your system. However, it is mainly presented in indigestible format, but you can use ConvertTo-HTML to create and format reports to analyze or send reports to someone.

You can use ConvertTo-HTML along with the output of another command using pipeline.  Get-Service | ConvertTo-HTML -Property Name, Status > C:Services.htm displays a list of all services and their status as web reports, stored in the “Services.htm” file.