Basic PowerShell Commands For Windows

Basic PowerShell Commands For Windows. Over the years, Microsoft has tried to turn PowerShell into one of the comprehensive management tools for Windows. Most of Microsoft’s server systems recommend that people use PowerShell, which can perform a lot of functions without having to intervene with the Command Prompt like before. Every Windows system administrator needs to be familiar with and use PowerShell from the very beginning. Here, Ngolongtech will introduce to you 24 basic commands when starting to get acquainted with PowerShell.

Basic PowerShell Commands For Windows
Basic PowerShell Commands For Windows

1. Basic PowerShell Commands: Get-Help

First and foremost, everyone needs to learn about any command, the syntax is Get-Help. For example, if you want to check Get-Process, type the following command:

Get-Help -Name Get-Process

and Windows will display the full syntax. In addition, Get-Help is also used with separate nouns and verbs, for example with the verb command Get:

Get-Help -Name Get-*

2. Set-ExecutionPolicy

Although you can create and execute different PowerShell scripts, by default Microsoft has turned off this feature to prevent different types of malicious code from entering the system. start up in a PowerShell environment. Users can apply the Set-ExecutionPolicy command to set different security levels, specifically there are 4 suitable options:

– Restricted : this is the default policy of the system, PowerShell commands are locked, users can only enter commands but cannot execute them.

– All Signed : if you or the administrator set the All Signed level, the code will be executed, but only for the explicitly specified components.

– Remote Signed : security policy when at this level, any PowerShell code generated inside the local system will be allowed to work. Code generated via remote is only allowed to run when fully assigned properties.

– Unrestricted : does not apply any form of restriction in the system.

The general syntax of this command includes the name of the Set-ExecutionPolicy command behind the policy. Examples are as follows:

Set-ExecutionPolicy Unrestricted

3. Get-ExecutionPolicy

If you have to work on an unfamiliar server system, it is important to know what level of security policy is being applied on it before executing any commands or code. To do this, you use the Get-ExecutionPolicy command .

4. Get-Service

This command will list all the services installed on the system. If you need to learn more about any service, add -Name and the name of that service, Windows will display the full details and related status.

5. ConvertTo-HTML

When you need to view or generate a complete report on the current state of the entire system, use the ConvertTo-HTML format conversion function . First, you need to specify the converted file path after using ConvertTo-HTML , the -Property parameter is responsible for initializing the properties in the HTML file, and finally naming the converted file. The general syntax of this command is as follows:

Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm

6. Export-CSV

After you create an HTML report based on PowerShell data, you can also extract the PowerShell data into a CSV file for use with Microsoft Excel. The general syntax is similar to the one above:

Get-Service | Export-CSV c:\service.csv

7. Select-Object

Using the above commands to learn about the system, you will discover that there are many attributes included in the CSV file. This feature proves to be really useful when it comes to allowing the user to specify fixed attributes in the associations. For example, to create a CSV file containing the names of individual services in the system and their associated status, you can use the following general syntax:

Get-Service | Select-Object Name, Status | Export-CSV c:\service.csv

8. Get-EventLog

Users can completely use PowerShell to analyze events occurring in the system through the log file. There are a few parameters specific to different services, but experiment by adding -Log in front of the log file name. For example, to view the Application log file , you use the following command:

Get-EventLog -Log "Application"

However, this syntax is not very common in working situations, where the user can choose between saving the report in HTML or CSV format.

9. Get-Process

Accompanied by the Get-Service command to display a list of the system’s current services, the Get-Process syntax is used to list all active processes.

10. Stop-Process

Sometimes, there are services in the system that fall into a “suspended” state. For such cases, use the Get-Process command to determine the exact name or ID of the process, and kill the process with the Stop-Process command . For example, to turn off the NotePad program , type the following command:

Stop-Process -Name notepad
Stop-Process -ID 2668

But be aware because the IDs of the processes will vary by system.

11. Get-Command

Get-Command
Get-Command

Windows PowerShell allows to explore its commands and features using Get-Command. It displays a list of commands for a specific feature or for a specific purpose based on the search parameter.

Just type Get-Command after your search query in PowerShell. For example, Get-Command *-service*show commands with “-service” in the name. Remember to use an asterisk at both ends of the query, as it is a wildcard that helps in finding the unknown.

12. Invoke-Command

When you want to run a PowerShell command or script – locally or remotely on one or more computers – “Invoke-Command” will be a useful companion. It is easy to use and helps you control a wide range of computers.

You must type 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 computer “Server01” use:

Invoke-Command -ScriptBlock {Get-EventLog system -Newest 50} -ComputerName Server01

13. Invoke-Expression

Invoke-Expression
Invoke-Expression

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

You must type Invoke-Expression followed by a command or an expression. For example, you can assign a variable “$Command” with a string indicating the command “Get-Process”. When you run Invoke-Expression $Command, “Get-Process” will be run as a command on the local computer.

14. Invoke-WebRequest

You can download, log in, and collect information on websites and web services while working in Windows PowerShell by using Invoke-WebRequest.

You must use Invoke-WebRequest followed by its parameters. For example, you can get the links on a certain web page using the command as:

(Invoke-WebRequest -Uri "https://docs.microsoft.com").Links.Href

15. Get-Item

Get-Item
Get-Item

If you are looking for information about an item at any location, such as a file on your hard drive, Get-Item is the best way to get that information in Windows PowerShell. You must be aware that it does not retrieve the contents of items, such as files and subdirectories in a given directory, unless explicitly specified by you.

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

16. Copy-Item

If you need to copy files and folders on your storage drive or registry entries and keys in the Registry, you can use Copy-Item. It works similar to the cp command in Command Prompt , but much better.

You can also use the Copy-Item command to copy and rename items in the same command – set the new name as the destination. For example, you can copy and rename “Services.htm” to “MyServices.txt” with the command:

Copy-Item "C:\Services.htm" -Destination "C:\MyData\MyServices.txt"

17. Remove-Item

Remove-Item
Remove-Item

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

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

Remove-Item "C:\MyData\MyServices.txt"

18. Get-Content

When you need to see the contents of a text file at a specific location, you open and read it in a code/text editor like Notepad++. In Windows PowerShell, you can use Get-Content to retrieve content without opening the file.

For example, you can retrieve the 50 lines of content of “Services.htm” with the command:

Get-Content "C:\Services.htm" -TotalCount 50

19. Set-Content

You can save text to a file using Set-Content , which is similar to the Bash shell’s echo command. Combined with Get-Content , you can also retrieve the contents of one file and copy it to another file with this command.

For example, you can enter Set-Content to write or replace the contents of a file with new content. Furthermore, you can combine it with the previous command’s example to save the output to a new file named “Sample.txt” using:

Get-Content "C:\Services.htm" -TotalCount 50 | Set-Content "Sample.txt"

20. Get-Variable

If you are looking to use variables in Windows PowerShell, the Get-Variable command will help you visualize variable values. It displays them as a table and allows the inclusion, exclusion and use of wildcards.

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

Get-Variable -Name "desc"

21. Set-Variable

You can assign or change/reset the value of a variable using the Set-Variable command. Besides, you can also set a simple variable using the format ${ $VarName = VarValue }$ like:

$desc = "A Description"

You can use the Set-Variable command followed by parameters to set a variable. For example, you can set a value for a variable named “desc” using the command:

Set-Variable -Name "desc" -Value "A Description"

22. Start-Process

Start-Process command
Start-Process command

Windows PowerShell makes it easy to start one or more processes on your computer. This command will be very useful in scripting applications, as it is one of the must-have commands to automate a task.

You can type Start-Process then the parameters to use the command. For example, you can start Notepad by typing the following command in Windows PowerShell:

Start-Process -FilePath "notepad" -Verb runAs

23. Start-Service

If you want to start a service on your computer, the Start-Service command can help you do the same from Windows PowerShell. It is 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” will start the Windows Search service on your local computer.

24. Stop-Service

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

Sponsored Links: