Updated March 23, 2023
Introduction to PowerShell
PowerShell is a Powerful task automation tool provided by Microsoft. Used as a configuration management framework for simplifying the configurations in the Windows ecosystem. It consists of a task-based command-line shell and scripting language. Designed mainly for system administrators. Helps IT professionals control and automate the administration of Windows Operating System and applications running on Windows. Built on top of the .NET framework and is based on object-oriented standards. cmdlet, a specialized, unique and single-function command-line tool comes into the picture with Windows PowerShell. All the administrative tasks are generally carried out by cmdlets that are capable of implementing certain operations and returning .NET objects.
How to Use PowerShell?
In all the latest versions of Windows, PowerShell can be found pre-installed. We just need to launch it through the given steps:
1. Click on the Search icon and find “PowerShell”.
2. We can see several options with PowerShell like
- Windows PowerShell
- Windows PowerShell ISE
- Windows PowerShell (x86)
- Windows PowerShell ISE (x86)
3. Select Windows PowerShell and right-click to “Run as Administrator”
While searching, apart from Windows PowerShell we get Windows PowerShell ISE as search results.
What is Windows PowerShell ISE?
- ISE refers to Integrated Scripting Environment.
- Windows PowerShell ISE makes scripting easier and more robust.
- It is a host application for Windows PowerShell, where we can run commands, debug our code, identify the issues to fix them and also test the code.
- PowerShell ISE contains a list of all the cmdlets and common modules required by the system administrators.
- PowerShell ISE is highly customizable allowing the users to choose color scheme, theme and font while working with scripts.
- Any script created in this ISE is given .ps1 file extension.
PowerShell cmdlet
- cmdlet or Command-let is a lightweight command used in the Windows PowerShell environment.
- cmdlets are created and invoked using PowerShell APIs.
- These are invoked by it in command prompt.
PowerShell cmdlet Examples
Let us understand how cmdlets work, through a few examples.
Code:
Get-Help
- This is the first command every administrator should be aware of.
- It helps you to know about other commands and how to use them.
For example, I want to understand how the Get-Process command works. So, the cmdlet would be
Get-Help -Name Get-Process
Get-Service
On running this cmdlet on the command line, a list of services on the machine is returned.
Get-Service “App*”
This cmdlet returns a list of services that begin with “App”.
Get-Service | Where-Object {$_.Status –eq “Running”}
This cmdlet further filters the list and displays only the services which are running.
Note:
- $_ refers to the current record in the pipe.
- It processes each output record returned by Get-Service checks if the status is “Running” and then accordingly filters the results.
Get-Service | Where-Object {$_.Status -eq “Running”} | Select-Object Name
We can further filter down the list to display only the second column, that is, Name column. This cmdlet will display only the names of the running services.
Get-Command
This cmdlet generates a list of cmdlets and functions installed in the machine.
Note:
- The syntax for Get-Command is VERB-NOUN.
- Verbs refer to Get, Set, Add, Clear, Write and Read
- Nouns refer to files, servers and several other items in the network or system.
PowerShell Scripts
- PowerShell scripts have file extension as .ps1
- For security reasons, by default, scripting has been disabled to prevent malicious code from executing in its environment.
- There are four levels of security available which one can see by running the:
Get-ExecutionPolicy command.
- Restricted: Default setting where no scripts are allowed to run.
- All Signed: Scripts will only be allowed to run if they are signed by a trusted developer.
- Remote Signed: Locally created scripts are allowed to run whereas, remotely created scripts are allowed to run only if they are signed by a trusted developer.
- Unrestricted: Any script can be run without any restriction.
Execution policy can be set by using the Set-ExecutionPolicy command followed by the name of the policy.
Set-ExecutionPolicy Unrestricted
One can find out the execution policy in use by running the Get-ExecutionPolicy command.
PowerShell Script Examples
There are majorly two ways of creating scripts and executing them.
Example #1 Open a notepad file and write the script directly to Then save this file with a filename with the extension .ps1
- Write the following line in a notepad file
Write-Host “My First PowerShell Script
- Then save this file as “ps1”.
- The script can be called in PowerShell by specifying the filename followed by the location where the file is
.\MyFirstScript.ps1
Example #2 Windows PowerShell ISE is another powerful way of creating PowerShell scripts and executing them. The benefit of using ISE is that it provides an environment to run, test as well as debug the scripts easily.
- Search for Windows PowerShell ISE and right-click to “Run as administrator”.
- Write the following code into the editor and save as “ps1”
Write-Host “My First PowerShell Script”
- Press F5 to run the code:
Understanding the Use of Variables
1. Open the PowerShell ISE and write the following code
$A = Get-Date
$A
Note:
- $A is a variable that stores the Date value.
- $A simply prints the value stored in it.
2. Save the file as “Date.ps1”.
3. Press F5 to execute the script and see the output.
Understanding the Use of “@”
1. Open the PowerShell ISE and write the following lines.
$array = @{Shape =”Square”;Side=4}
$array
Note:
- “@” symbol refers to an array.
- $array is a variable that contains an array.
2. Save the file as “Array.ps1”
3. Press F5 to run the script and see the output.
Conclusion
In this article, we tried to cover almost all the basic ideas on it and hopefully, this will give a clear picture of how to use it. In today’s tech industry, it has become a powerful tool and an ideal choice for IT professionals and administrators due to its great flexibility and contribution to simplifying Management operations. There are numerous tasks one can perform with the help of ps, even those of which you might not be aware. The more you start using it, the more you will start learning the capabilities of this powerful tool.
Recommended Articles
This is a guide to How to Use PowerShell?. Here we discuss what is PowerShell, ISE, and scripts with their examples and use of it. You can also go through our other related articles to learn more –