PowerShell is to Windows, what a terminal is to Linux. It’s a powerful automation and scripting tool that comes pre-installed with all new Windows releases. In addition to running commands, PowerShell also accepts and returns .NET framework objects. This opens the door to a whole new world of tools and solutions to manage and configure Windows.
An Intro to Cmdlets
Command-lets (cmdlets) are lightweight, single-purpose commands used to interface with PowerShell. Written in a .NET programming language, they are compiled and stored as a .dll file. Usually they execute a single operation like copying files, moving directories, or displaying help. Cmdlets can be executed directly from the shell, or from a PowerShell script. An example of a cmdlet is: get-help, which is used to fetch information about other cmdlets. Cmdlets can accept input parameters as well; e.g. get-help add-appxpackage will return helpful information about the appxpackage cmdlet.
Cmdlets vs. Functions
A PowerShell function is an element that groups code and has optional input and output parameters. Unlike a cmdlet, they aren’t compiled. They can be executed directly from the command line, or specified in a script or a module. An example of a function is Get- Start-PSAdmin which starts PowerShell as an administrator.
How to Launch PowerShell
As mentioned above, PowerShell comes installed by default in all new Windows versions. To launch it, follow these steps:
- Click the Windows button.
- Search for “PowerShell”
- Select “Windows PowerShell” from the search results.
Some Handy PowerShell Command-lets
Now let’s look at some of the most basic command-lets that you should know about:
- Get-Command:
This command-let is used to display all the commands and functions available for you to use in your current session. Depending on your user privileges, the number of commands/functions might differ. For example, if you have administrative rights, you might have access to a lot more commands and functions than a basic user.
A typical output might look like:
- Copy-Item
The copy-item command-let (alias: copy) allows users to copy an item from one place to another. The syntax of the command is as follows:
Copy-Item “C:\Tom\Logfile\mylog.txt” -Destination “C:\junk”
- Set-ExecutionPolicy:
By default, Microsoft releases PowerShell with scripting disabled. This is to prevent hackers from running scripts in the background without your knowledge. However, if you want to turn on scripting, you can set either one of the four available security levels:
- Restricted: Only enter commands through the CLI. Can’t run scripts.
- All signed: Only scripts that have been signed by a credible publisher will be allowed to run.
- Remote signed: All locally created scripts will be allowed to run. Remotely published scripts will only be run if they have been signed by a credible publisher.
- Unrestricted: All scripts will run.
- Get-EventLog:
If you are interested in reading any application logs, you can use the “Get-eventLog” cmdlet. The syntax would be as follows:
Get-EventLog -Log “Application”
- Get-Service:
To know what services are currently running on the system, you can use the “Get-service” cmdlet.
You can also ask PowerShell to tell you whether a service is installed on your system. To do so, append the -Name directive before the name of the service.
- Clear-History:
It’s also possible to clear your PowerShell command history. Simply use the “Clear-History” command-let. You can also clear a certain set of commands by specifying them as inputs to the “Clear-History” cmdlet. The syntax would be as follows:
Clear-History -Command *get*, *app
The above will clear all commands that have the words “get” and “app” in them.
- ForEach-Object:
Just like the “ForEach” function of container datatypes in programming languages, the “ForEach-Object” cmdlet in PowerShell allows you to execute an operation on every item in a list. So, let’s say you want to print out the names of different running processes in red. You can do that using the following command:
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor red}
- Clear:
Sometimes, after running a bunch of commands on the terminal, you just want to clear the screen. You can do that using the “clear” cmdlet.
- Compare-Object:
If you are a developer working with PowerShell, the “compare-object” command-let is one you certainly want to master. As the name indicates, it allows you to compare two objects directly. Most of the time you will use it in a script, but you can also use it directly via the CLI as follows:
- New-Object:
You can also create a .NET or a Component Object Model (COM) object on PowerShell. The “New-Object” cmdlet creates objects that can be manipulated just like normal .NET objects.
myObject = New-Object -TypeName psobject
These were only some of the highly used PowerShell commands you should know about. There are plenty more to explore. Find them here.
PowerShell Scripting:
Scripts are used to harness the maximum potential of PowerShell based system administration. PowerShell scripts are just text files stored with an extension of .ps1. Follow these steps to create your first PowerShell script:
- Click the “Windows” button and enter “Notepad”.
- Open notepad and type Write-Host ‘Hello, World!’
- Click “Save as” and save the file with the extension “.ps1”
- Now open PowerShell and use the “cd” command to navigate to the directory where you saved the .ps1 file. E.g. if you saved it to your desktop, you can use the following command:
cd C:\Users\alex.pc\Desktop\
Replace “alex.pc” with your username.
- To execute your script, type “./scriptname” and hit enter. The screen should display “Hello, World” as can be seen in the screenshot below:
You can group different commands in your script to create a comprehensive administration/automation tool. If you want to run a script periodically, you can do so using the ScheduledTasks group of cmdlets. More information on them can be found here.
Final Word:
PowerShell is a powerful scripting and automation tool that makes Windows systems easy to administer and monitor. Whether you want to run background jobs, or just want to move a bunch of directories around, you can do it all using PowerShell.
Leave a Reply