Thursday, September 5, 2019

PowerShell Hosts

What is a PowerShell Host?

Generally, a host is something which stores or holds information on which other components depend to function properly. So a PowerShell host is nothing but a tool or a software which has all the necessary files which allows us to execute the PowerShell commands. PowerShell is the engine and hosts will allow us to use PowerShell engine to work with all the available commands. 
The host may also provide us with enhanced GUI and certain features to develop, debug or version control mechanism for our PowerShell Scripts. For example, the simplest host we can mention here is the PowerShell Console and PowerShell ISE which are provided by Microsoft as out-of-the-box tools in Windows 7 and later clients. Some third party hosts which can be really helpful and worth mentioning to work with PowerShell scripts are PowerGUI, PowerShell Plus, Primal Script, PowerShell Studio, Visual Studio Code and even Visual Studio. Some of them are paid software and some of them are freeware. So, depending on the kind of work you are going to do and the features required, we can go with any of these tools. For my posts here, I’ll be using Visual Studio Code. I started using VS code recently to work with PowerShell Scripts and it’s my new love.

However, I would like to give a brief introduction to the default hosts provided to us by Microsoft in our Windows operating systems. (May or may not be present by default in some versions of Windows Server OS, but nothing that we cannot do to install it).
In a Windows Operating system, you will find the basic PowerShell Console and PowerShell ISE shortcuts in the Start menu itself. In a 32-bit OS, the two shortcuts will be available and they are the 32-bit version of PowerShell. However, in a 64-bit OS, 4 different shortcuts are available as shown below:


Fig 1.1 : PowerShell Shortcuts from the Windows Start Menu


PowerShell Console Vs PowerShell ISE

PowerShell Console allows you to use all the commands available in the PowerShell Engine. However, it does not give you with a script editor. PowerShell ISE on the other hand gives you the same features as PowerShell Console plus a script editor where you can open multiple scripts at a time and work on those.

Fig 1.2 : PowerShell Console (Administrator mode)
Fig 1.3: PowerShell ISE(Administrator mode)

In the above screenshots you can see that in the title bar, it is displayed as “Administrator:  Windows PowerShell” and “Administrator:  Windows PowerShell ISE”. This is because I have started them as “Run as Administrator”. Now this elevated privilege is required to perform many actions from within PowerShell without which you will get errors like “Access denied” or other permission related issues. For example, if I want to modify the contents of a file which is located in my Root System directory or C: drive, executing the script without starting the PowerShell Console or PowerShell ISE in administrator mode will give you “Access denied” error. The normal start ups of these hosts will have titles without Administrator prefix.

Fig 1.4 : PowerShell Console (Normal mode)

Fig 1.5: PowerShell ISE(Normal mode)

So depending on the actions you want to perform with your scripts, you can use any mode of the hosts as required.

POINTS TO REMEMBER:
  • PowerShell ISE comes with a toolbar where you can find many tools which will easy your life while working with the scripts.
Fig 1.6: PowerShell ISE toolbar
  • Use the x86 or x64 version of the hosts depending on your system architecture. They are designed to work best in the same architecture. 
  • Use Debugging mode and Debuggers in PowerShell ISE to debug your scripts.
  • Use shortcuts such as 
    • F5 to run the script
    • F8 to run a selection of the script
    • F10 to go to next statement in debugging mode
    • Make use of the Environment Variables for various usage.



I’ll discuss on all the points in my upcoming posts.
Happy Scripting :)


No comments:

Post a Comment

Start from here

PowerShell Hosts

What is a PowerShell Host? Generally, a host is something which stores or holds information on which other components depend to functi...