PowerShell IDE

This can be invoked by clicking on the Start button and searching for Windows PowerShell ISE. Once invoked, the initial screen will look like this:

As we can see in the preceding screenshot, a PowerShell script is saved with a .ps1 extension. Once we write something in the IDE (or ISE, as it is called with PowerShell), it needs to be saved as somefilename.ps1 and then executed to see the result.

Let's take write a program called Hello World:

  • As we can see in our first program, we write two lines to print Hello World. In the ISE, we pass the commands to declare a variable (a variable is denoted by a dollar sign, $, in front of the variable in PowerShell), assigning the value Hello World to it. The next line is simply printing that variable by calling a method or function called Write-host, which is used to print values onscreen in PowerShell.
  • Once we write the program and save it, the next step is execution to see our result.
  • The green button at the top of the ISE is used to execute the script, and the result of the script is shown at the bottom of the screen. In this case, it was a simple Hello World output.

PowerShell scripts can also be invoked directly by the command line. As PowerShell is a scripting language and needs to be installed on a machine, we can directly call PowerShell from the Windows Command Prompt and execute the scripts and individual scripting commands from the PowerShell console itself.

This is how we can find out the version of PowerShell:

As we can see in the preceding screenshot, PowerShell is invoked by calling powershell directly from the Command Prompt in Windows. When PowerShell is invoked, we see PS before the command line, which confirms that we are now inside the PowerShell console. To see the version, we call a system variable, $psversiontable, which shows the version of PowerShell.

We can see that this is version 2.x (as shown in CLRVersion). System variables are special variables that have predefined values based upon the installation types. These special variables can be called at any time in our script to fetch their values and perform actions based upon the returned values.

The following example shows that we are using a higher version of PowerShell:

As we can see, the same variable now returns a value of 4.0 for PSVersion, which confirms that this is version 4 of PowerShell.

PowerShell 4.0 is the default installation from Windows 8.1 onwards on client operating system, and Windows Server 2012 R2 in a Server environment.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset