What is PowerShell?
PowerShell is an automated object-oriented scripting language. It is widely used by system administrators. PowerShell is built on.Net framework. It uses common language run-time and this framework accepts and returns .NET framework objects. It also introduces a feature of cmdlet which is a simple command line tool that is built into a shell. It is an 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 can be used by any Windows admin. 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 user can make use of this command and then change it to any of the below:
- Restricted: This is the default Set option which locks PowerShell so that commands can be entered only interactively.
- All signed: This mode allows all scripts to run only when they are signed by a trusted publisher.
- 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 then they must understand the execution policy which is used to run the script and this can be found by making use of the Get-Execution policy.
To get a list of all running processes this command can be used which will display all currently running processes.
Sometimes it may happen that a process is stuck. By making use of Get-Process the id of the process 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.
6. Select Object
Whenever an object is exported there are numerous properties that are included in the CSV file. 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 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. It provides many parameters that help in finding the log by providing -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 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. Users can use a Property switch which helps in controlling the output which includes HTML file.
This command provides a list of all services which can be installed. If the user wants some specific service, then they can append the -Name switch and then name the service.
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, which have particular property values from a collection of objects which are 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. User 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 advances commands, and tips and tricks to use PowerShell Commands. You can also go through our other Suggested Articles to learn more –