Friday, September 6, 2019

PowerShell Help

If we want to learn how to drive a car, we need someone who can teach us to learn driving a car. While if we want to use a software, we need to know how the software works and the first thing we start looking for is the HELP menu inside the software. Similarly, when we start working with PowerShell, there is a HELP system already provided to us by the PowerShell Engine which we can look upto anytime using some commands to learn or get help on the various commands available to us.

So the question is how do we reach out to PowerShell Help System !?
The answer is very simple: Get-Help. Yes, this is the very first command you can ask the PowerShell Engine to get help for any commands available.
But before we start taking help from the PowerShell Help System, it would be wise to keep the Help System updated so that we get the  most recent version of the information.

Now How do we update the PowerShell Help System?
Again the answer is quite simple. Run the Update-Help command in the PowerShell Host, say for now the PowerShell Console. But there's a catch to this. Update-Help will only work if your machine is connected to the internet and you have started the host as Run as Administrator. 
So few points to remember here while using Update-Help command is:
  • Make sure the system is connected to the internet.
  • Start the PowerShell Host as Admin.
  • Execute Update-Help
If you do not start the PowerShell Host as Admin, you may see Access is denied error during the update.

What if there is no internet connection? Can not we update the help system at this point? If yes, how?
Answer is yes, we can definitely update the help-system. 
Suppose that your and your friend need to complete a homework and your friend has already got the answer bank. So what you can do is you can take printouts of the answer bank and keep it one copy for yourself so that you can look up to it anytime.
Same is the case for PowerShell Help System. If one machines already has updated Help contents, then you can use the Save-Help command to make a copy of all the updates help contents. Then you can use this copy as a source to update the Help system of your machine where there is no internet connection.

Save-Help command looks like this;
Save-Help -DestinationPath "C:\Help" -Force

where C:\Help is the folder where all the XML files are saved, -Force can be used if you want to overwrite the folder if already exists.

and the folder contents look like this:
Fig: Save-Help Destination Folder contents

Now that we have all the help files downloaded, how do we update our machine using them.
Command looks like this:
Update-Help -SourcePath "C:\Help"

So these are the ways how we can update our PowerShell Help System.

Now How do we use Help?
Suppose you want to find out the list of services running in your machine but you are not sure which command is available in the PowerShell to get the list of the services. This is where you need Help.
See the below example to get a list of commands available to related to services:

Here you can see the commands and you can start playing with those.
If you are not sure how each command works, Help is there for you again. For example if you want to find out about Get-Service, try something like "help Get-Service" and PowerShell Help will show you everything you need including syntax, description, parameters and examples.
Fig: Help Get-Service

In this manner, you can try out other keywords which can be noun or verb and then deep dive into specific commands.
Few other ways of using Help are with the additional parameters like:
  • To see the examples, type: "get-help Get-Service -examples".
  • For more information, type: "get-help Get-Service -detailed".
  • For technical information, type: "get-help Get-Service -full".
  • For online help, type: "get-help Get-Service -online"
I have copied and pasted the above information from PowerShell Help only. :P
So keep scripting guys... Stay tuned! :)

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 :)


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...