Windows PowerShell commands – Part 2 - HostAfrica
+27 21 554 3096

Blog

Windows PowerShell commands – Part 2

Windows PowerShell commands – Part 2

Benefits of PowerShell over Command Prompt

Well, PowerShell certainly has more power! The command prompt is an interface available to execute simple DOS commands; most users have not explored it beyond ping, ipconfig or in the programming world, ftp. However, PowerShell is much more than that. While there are many differences between the two, here are a few important ones:

  1. PowerShell uses cmdlets, not commands. Cmdlets are not just a different way of calling the same thing, but they expose complex system administration functionalities such as registry management and Windows Management Instrumentation (WMI) to the user. This makes them far more useful than the command prompt.
  1. PowerShell is object-oriented. The data outputs from cmdlets are objects (an example of how object-orientation makes PowerShell attractive) and not just text. This provides more flexibility to play around with complex data.
  1. PowerShell is developed using the .NET framework. This allows PowerShell scripts to use .NET interfaces and extend features that are not provided by default through cmdlets. The other way around is also possible – embedding PowerShell scripts in .NET code.

PowerShell Cmdlet Examples

On the command line, when you run the Get-Service cmdlet, you get a list of services on your machine.

You can further filter these just to show the services that are running:

Get-Service | Where-Object {$_.Status –eq “Running”}

Here, PowerShell processes every record output that GetService throws evaluate whether the ‘Status’ attribute is ‘Running’, and filters accordingly.

Note: $_ refers to the current record/object in the pipe.

Some more advanced usage…

PowerShell scripts offer a handy way to automate tasks. Here are some concepts that will help you as you start developing PowerShell scripts.

PS1 files

A PowerShell script is really nothing more than a text file. The file contains a series of PowerShell commands, with each command on a separate line. For the text file to be treated as a PowerShell script, its filename needs to end with the .PS1 extension.

Running a script

In the past, if you wanted to run an executable file from the command line the practice was to navigate to the file’s path and then type the name of the executable file. However, this doesn’t work for PowerShell scripts.

If you want to execute a PowerShell script, you will usually have to type the full path along with the filename. For example, to run a script named SCRIPT.PS1, you might type:

C:\Scripts\MyScript.ps1

The big exception is that you can execute a script by simply typing its name if the folder containing the script is in your system’s path. There is also a shortcut you can use if you are already in the folder containing the script. Instead of typing the script’s full path in such a situation, you can enter .\ and the script’s name. For example, you might type:

.\MyScript.ps1

Pipelining

Pipelining is the term for feeding one command’s output into another command. This allows the second command to act on the input it has received. To pipeline two commands, simply separate them with the pipe symbol “|”.

To help you understand how pipelining works, imagine that you want to create a list of processes that are running and sort that list by process ID. You can get a list of processes by using the Get-Process command, but the list will not be sorted. However, if you pipeline the cmdlet’s output into the Sort-Object ID command, the list will be sorted. This looks like this:

Get-Process | Sort-Object ID

Variables

Although you can use pipelining to feed one command’s output into another command, sometimes pipelining alone is not enough. When you pipeline a command’s output into another command, that output is used immediately. Occasionally, you may need to store the output for a while so that you can use it later. This is where variables come into play.

It’s easy to think of a variable as a repository for storing a value, but in PowerShell, a variable can store a command’s full output. For example, suppose you want to store the list of processes running on a server as a variable. To do so, you could use this line of code:

$a = Get-Process

Here, the variable is named $a. If you want to use the variable, simply call it by name. For example, typing $a prints the variable’s contents on the screen.

You can assign a variable to the final output of multiple commands that have been pipelined together. Just surround the commands with parentheses. For example, to sort the running processes by process ID and then assign the output to a variable, you could use this command:

$a = (Get-Process | Sort-Object ID)

 

In Summary

We can never cover the full potential of PowerShell in just a few blog articles, but hopefully, these articles have given you a taste of what is possible.

Host AfricaHappy Hosting!

Leave a Reply