Powershell provides a rich scripting environment in Windows based operating systems. It provided better scripting options than Windows batch programming and VBScript which were previously used. It is high level language in itself as the script is not interpreted, but compiled to MSIL and then executed. Nearly all Windows based servers are fully supported by Powershell scripting. Any task or action done with the help of GUI can now be automated using Powershell modules.
Powershell is based on .NET Framework. It has nearly all the features available which are available in any .NET Framework related programming languages such as C#, VB.NET, VC++ and others. It also allows running programs written in .NET based languages.
Powershell was initially named as Monad, but it was later named as Powershell.
The programming style of Powershell is similar to Perl and Korn shell and similar to C# or VB.NET.
Powershell can be used in Windows Core installation for administrative purposes.
How to install Powershell?
There is no need to install Powershell on Windows 2008 and above version of servers. You just have to enable the feature.
To check if Powershell is installed-
1. Goto Start > Run.
2. Type cmd to open command prompt.
3. Type below command -
where powershell
where powershell
If Powershell is not installed, you will get below output-
INFO: Could not find files for the given pattern(s).
You may install the version 1 or version 2 of Powershell based on the machine type and operating system. Please search on the internet and find the link, I am skipping providing link as it might change in future.
Powershell command line
Powershell command line is nearly the same as Command prompt. However, it looks bit different in background color which is blue by default.
Hello world!
This is simple to just write Hello world! Just type in below command -
echo Hello world!
But, I will write a script for the same which will have an argument.
Writing your first script in Powershell:
1. Powershell script must be a script with *.ps1 extension.
2. Your execution policy must be set correctly. I will discuss about execution-policy later, but for now, set to unrestricted to run your first script using below command.
set-executionpolicy Unrestricted;
3. Write the script as mentioned below -
function HelloWorld([string] $useInput) { echo $userInput; } HelloWorld $args[0];
4. Save the script. I assume your named helloworld.ps1.
5. Run the script
From Powershell command-line:
.\helloworld.ps1
Use .\ when the script is located on the same directory (present working directory)
You may also write the complete path.
From Command prompt:
Powershell D:\helloworld.ps1
Points to remember :
1. The file extension must be .ps1
2. Powershell execution policy must be set appropriately to run the script on a given machine.
3. You cannot double click the powershell script similar to batch programs.
4. A Powershell script run under .NET Framework. It can include all the libraries which C# or VB.NET can utilize.