What is PowerShell?
PowerShell is an automated object-oriented scripting language. System administrators widely use it. PowerShell is built on.Net, a framework. It uses 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. Let us have a look at the different commands which can be used in PowerShell.
Basic PowerShell Commands
The following are the basic PowerShell commands:
This is the very basic and most important command which any Windows admin can use. This command can be used to get help with other commands. In order to get information about other commands, this command is useful. Windows will display the full command syntax.
2. Set-Execution Policy
In order 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 below:
- Restricted: This is the default Set option that 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: This means all restrictions will be removed from the execution policy.
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.
This command can be used to get a list of all running processes, which will display all currently running processes.
Sometimes it may happen that a process is stuck. By using Get-Process, the process’s id can be found, and the user can terminate this process by using the Stop-Process command. The name of the process id can be used to terminate the ongoing process.
Whenever an object is exported, there are numerous properties that are included in the CSV file. The select command helps you in narrowing down things that will include objects in which only you are interested. This command helps you to specify some specific properties for inclusion. For example, for creating a CSV file with the name of each system and its status, the following command can be used.
7. Get-Event Log
To parse the computer event logs, PowerShell is widely being used. This is because it provides many parameters that help in finding the log by providing a -log switch which is followed by the name of the log file.
8. Export CSV
The data present can be exported from PowerShell into a CSV file that can be accessed using Excel. The user needs to provide the output filename, and this can be exported to a CSV file.
9. Convert To-HTML
PowerShell can provide varied information about a system and helps in creating a report which can be sent to anyone. This can be accomplished by making use of Convert To-HTML. To make use of this command 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.
This command provides a list of all services which can be installed. 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 explained briefly:
1. Dir: It gets all files and folders in a file system drive.
2. Epsn: This command imports commands from different sessions and saves them in the Windows PowerShell module.
3. fl: It formats the output list of properties in which all properties appear on a new line.
4. Get-job: It gets the Windows PowerShell background jobs that are running in the current session.
5. Get-member: It helps in getting properties and methods of the object.
6. Get-Item Property: It gets the property of a specified item.
7. Invoke-Command: It runs the commands on local and remote computers.
8. Invoke-WMI Method: It calls Windows Management Instrumentation methods.
9. New-PSDrive: It creates temporary and persistent mapped network drives.
10. Out-Host: It sends output to the command line.
Advanced PowerShell Commands
These advanced commands for PowerShell are useful for obtaining information in different formats and configuring security; let’s look at a few of the commands as mentioned below.
1. Get-Unique: This is a cmdlet that fetches unique objects from a sorted list of objects.
2. Measure-Object: This cmdlet can be used to get properties of passed output for functions like min, max, size, count, line, etc.
3. Where-Object: This cmdlet is used to select objects that have particular property values from a collection of objects passed to it.
4. Start-Sleep: This is also a cmdlet that suspends the activity present in a script or session for a particular period of time.
5. Invoke-Expression: It is used to perform 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 in order 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.
PowerShell Commands is a very powerful tool which helps the admin for monitoring and other purposes. It is analog in Linux and is also known as bash scripting. It provides cmdlets which help in managing the computers through the command line. It also provides access to data scores like registry and certificate score. It also has a rich parser which helps in developing scripting language. Hence it helps in managing all activities with ease of commands.
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 –