Introducing PowerShell ISE – Chapter 1

Remember PowerShell?? It is a shell framework developed by Microsoft to automate repetitive tasks. By default, this framework is available with Windows 10. But in earlier versions, this framework had to be installed manually. PowerShell now comes with an ISE- an integrated scripting environment. This ISE is a Windows host application that enables users to write, test, execute and debug scripts- all in one window. This host application was introduced with PowerShell version 2 and then was improvised in PowerShell V3. The latest version of this application is V5.x

Key features of PowerShell

  1. It uses cmdlets and not just simply commands, allowing complex system administration functionalities
  2. It is object-oriented providing more flexibility to work the with complex data
  3. Extends and leverages features of .NET framework

Opening PowerShell ISE-

Step 1 -> Search for “PowerShell ISE” application as shown below and open this application with option “Run as Administrator”

Step 2 -> The application has three key segments as shown in figure below-

The SCRIPTING Window is where you can create scripts and save them with “*.ps1” extension. When you run scripts, the result gets displayed in the below window – we can call it as the EXECUTE or RUN or DEBUG window. And the COMMANDS window displays list of cmdlets that can be imported and used (these are extensions from .NET framework).

Step 3 -> Lets run a simple cmdlet and view list of services running on your machine. Type in the cmdlet “Get-Service” in the bottom window as shown below-

The above command returns list of services on your system that are running or are in stopped state. You can further filter out the results using the “| where-object“ clause … like you want to view list of services that are in stopped state….you can type in the below statement-

Get-Service | where-object {$_.Status -eq “Stopped”}

Apart from just mentioning the criteria, you can further select specific table columns using the command “|Select-Object <<column-name>>”, like

Get-Service | where-object {$_.Status -eq “Stopped”} | Select-Object Name

The “|” allows concatenation between the clauses. This is very similar to the way we run SQL commands- where you fetch records, then filter the records and mention the column names that need to be displayed.

The ISE also provides HELP on cmdlets. Use the following command to check out the purpose of the “Get-Service” cmdlet-Get-Help -Name Get-Service

In case you are looking for more details on a cmdlet, then you can append “-Online’ keyword to access online help.

Get-Help-Name Get-Service – Online

One interesting thing, you would have observed that the cmdlet follows a VERB_NOUN syntax and this makes these cmdlets easy to remember. There is another way to remember these combos and those are called “ALIAS”. As you see in the above snapshot, the “Get-Service” cmdlet has an alternate name called “gsv”. This means you can simply type in alias instead of typing the whole cmdlet. So instead of typing “Get-Service”, the command “gsv” will also return the same result.

The results returned by cmdlet can further be analyzed and filtered. For example – you want to view list of dependent services running on your system, lets take a look-

Get-Service| where-object {$_.DependentServices} | Format-list -Property Name, DependentServices, @{Label=”NumberofDependentServices”;Expression={$_.dependentservices.count}}

The above command performs the following tasks-

  • Fetches all services that have dependent services
  • Formats the list to return a group of records comprising of the service name followed by the name of the dependent service and then finally the number of such dependent services

Take a look at some other simple cmdlets to start with-

CMDLETPurpose
GET-SERVICE -NAME <<servicename>>

Return the description and alias for the mentioned service
GET-PSDRIVE
Display the current PowerShell drive
WRITE-OUTPUT <<value to be displayed>>Display mentioned text
START-SERVICE -NAME <<servicename>>
Initiate a service for mentioned name
STOP-SERVICE -NAME <<servicename>>Stop the mentioned service
NEW-SERVICE -NAME <<servicename>> -BinaryPathName “<<location of exe file>> -k netsvcs”Register an exe as a service from the mentioned location
NEW-ITEM -PATH “location/foldername” -ITEMTYPE DIRECTORYCreate a new folder at the mentioned location
NEW-ITEM -PATH “location/foldername” -ITEMTYPE FILECreate a new file at the mentioned location
COPY-ITEM “<<sourcelocation/filename>>” “<<destinationlocation/newfilename>>”Copy file from source to destination
GET-DATEView system date and time
GET-CONTENT “<<folderlocation/filename>>”Read and view file content from mentioned location
TEST-PATH “<<location/filename>>”Check file existence and return True/False

Try out these basic cmdlets and in the next chapter (Chapter 2) we will learn the steps to create scripts in PowerShell ISE.

Happy Learning !