Updated June 1, 2023
What are PowerShell Commands?
The following article provides an outline for PowerShell Commands. PowerShell is an automated object-oriented scripting language. System administrators widely use it. PowerShell is built on .Net, a framework. It uses a common language run-time, and this framework accepts and returns. NET Framework objects. It also introduces a feature of a cmdlet, a simple command-line tool built into a shell. It is open-source and cross-platform. The cmdlets can access data from different data stores, like a file system or registry. The cmdlets can be added by a third-party provider as well.
Basic PowerShell Commands
The following are the basic PowerShell commands:
1. Get-Help
This is the most important command any Windows admin can use. This command can be used to get help with other commands. To get information about other commands, this command is useful. Windows will display the full command syntax.
2. Set-Execution Policy
To prevent any malicious code from executing in the PowerShell environment, Microsoft has disabled scripting.
To control this level of security, users can make use of this command and then change it to any of the following:
- Restricted: This default Set option locks PowerShell to enter only interactively.
- All signed: This mode allows all scripts to run only when a trusted publisher signs them.
- Remote signed: This mode allows locally created PowerShell scripts to run if this option is set.
- Unrestricted: By doing so, the execution policy will have all restrictions lifted.
3. Get-Execution Policy
When the admin is not familiar with the server, they must understand the execution policy used to run the script, which can be found by using the Get-Execution policy.
4. Get-Process
This command can get a list of all running processes, displaying all currently running processes.
5. Stop-Process
Sometimes a process may be stuck. Using Get-Process, the process’s id can be found, and the user can terminate this process using the Stop-Process command. The name of the process id can be used to terminate the ongoing process.
6. Select-Object
Numerous properties are included in the CSV file whenever an object is exported. The select command assists in filtering and narrowing down specific objects of interest. This command allows you to specify some specific properties for inclusion. For instance, you can utilize the following command to generate a CSV file containing the system names and their respective statuses.
7. Get-Event Log
To parse the computer event logs, PowerShell is widely used. This is because it provides many parameters that help in finding the log by providing a -log switch followed by the log file’s name.
8. Export CSV
Exporting the data from PowerShell into a CSV file is easy and can be accessed easily using Excel. The user needs to specify the output filename to which the data can be exported as a CSV file.
9. Convert To-HTML
PowerShell can provide system information and generate reports that anyone can share. You can achieve this by utilizing the Convert To-HTML function. To use this command, the user can easily pipe the output from another command into Convert To-HTML. In addition, users can use a Property switch, which helps control the output, which includes an HTML file.
10. Get-Service
Executing this command will generate a list of all installable services. If the user wants some specific service, they can append the -Name switch and then name it.
Intermediate PowerShell Commands
Below is the list of Intermediate PowerShell Commands:
- Dir: It gets all files and folders in a file system drive.
- Epsn: This command imports commands from different sessions and saves them in the Windows PowerShell module.
- fl: It formats the output list of properties in which all properties appear on a new line.
- Get-Job: It gets the Windows PowerShell background jobs running in the current session.
- Get-Member: It helps in getting the properties and methods of the object.
- Get-Item Property: It receives the property of a specified item.
- Invoke-Command: It runs the commands on local and remote computers.
- Invoke-WMI Method: It calls the Windows Management Instrumentation method.
- New-PSDrive: It creates temporary and persistent mapped network drives.
- Out-Host: It sends output to the command line.
Advanced PowerShell Commands
These advanced commands for PowerShell help obtain information in different formats and configure security.
- Get-Unique: This is a cmdlet that fetches unique objects from a sorted list of objects.
- Measure-Object: This cmdlet can get properties of passed output for functions like min, max, size, count, line, etc.
- Where-Object: This cmdlet selects objects with particular property values from a collection of objects passed to it.
- Start-Sleep: This is also a cmdlet that suspends the activity in a script or session for a particular period.
- Invoke-Expression: It performs a command or expression to the local computer.
Tips and Tricks to Use PowerShell Command
- Make use of unique $variables. The variables can store and manipulate the data. It increases the flexibility and reuse of code.
- Use Start-Transcript and Stop-Transcript to enable logging in your scripts.
- Make use of Try and Catch to improve script resilience.
- Use Import-CSV and Export-CSV links for importing and exporting data. You can make your own scripts.
- For repetitive actions, use for each. Users can repeat blocks which can help in automating large manual tasks.
Conclusion
PowerShell Commands is a potent tool that helps the admin for monitoring and other purposes. It is analog in Linux and is also known as bash scripting. PowerShell offers cmdlets that enable command-line management of computers. It also provides access to data scores like registry and certificate scores. It also has a rich parser which helps in developing scripting language. Hence it helps in managing all activities with ease of command.
Recommended Articles
This has been a guide to PowerShell Commands. Here we discussed the concept, basic to advanced commands, and tips and tricks to use PowerShell Commands. You can also go through our other Suggested Articles to learn more –