Believe me or not, but that’s true! The time was already passed and we have to run!! I am talking about Learning PowerShell. Although, I am using PowerShell Scripts for quite sometimes, I didn’t pay a lot more attention to learn in details (probably, it was not required that time ). You probably thinking, “Wait a minute, you said, you are using PowerShell scripts, but you didn’t know PowerShell?” Correct, I learned three command-lets (cmdlet), after which I was able to understand few scripts, which I am using at work. This post, is all about learning three PowerShell cmdlet, which will help you to learn the rest of them.
Things you should do as you begin your journey with PowerShell
Although, I believe it’s our individual interest, whether we want to learn PowerShell or not, here are the few things, which can boost our learning.
- Stop using cmd.exe !! Yes, this is the biggest obstacle in your learning. I am sure, most of us (including me ) run this tool more than once in a day, since last few years. The good news is, most of the commands, which we run in cmd.exe (e.g. ping/ipconfig/robocopy/mstsc, etc.), is also available in PowerShell (or a better updated version with more features)
- List down the activities, we repeat everyday and try to automate it. As we are updating SQL Server 2008 R2 Service Pack2, we had to check all the servers to make sure, we have the enough free space available to complete the SP2 update. Believe me or not, with the help of PowerShell, we extracted the information in a few minutes !!! Last time, when we did it manually, I think, we spent nearly 12 hours to collect that information .
- When in doubt, ask for help! A quick way is the twitter hash tag #help #PowerShell. Apart from that, you will find several online forums, where you can post your query and someone from the community will come forward to help you (it’s normally nice, if you tell them not only about the task you want to automate; but also the way you had already tried ).
- Build a test lab. Create a virtual machine on your personal laptop/desktop, for your learning. Since PowerShell is a very powerful tool, it’s possible, that you can unknowingly commit some mistakes, which may result in loss of data . However, if you are using a VM, you don’t have that fear. ( I know, how it feels, if your old photos got deleted ). Please … Please.. Please, Don’t use the Production environment for your learning.
Enough of the talks !!! Let’s proceed with, the three cmdlets.
Three Cmdlets to Start
The first command, I learnt to run on cmd.exe was help. As with most other commands, help is also available in PowerShell. You can type help/Get-Help/man, to start the help subsystem. If you are running PowerShell v1/v2, when you run Get-Help, you will get the complete help subsystem. However, if you are running PowerShell V3 or above, the help subsystem is not available by default, you have to install it manually. To install the help file on a system running PowerShell v3, run the following command (make sure, you have opened the PowerShell console as an administrator )
# Update & Install the Help files for all the modules, with language as EN-US
Update-Help -Module * -UICulture "en-us" -Force
The Help subsystem is our friend & mentor in our journey with PowerShell. Spend some time with Get-Help, so that you can go well together.
In most of the cases, a PowerShell cmdlet is a combination of a verb & Noun (you missed the grammar class in school, time to learn it again ). There is a list of approved verbs (98 in PowerShell v3), which can be used in PowerShell. As you might think of, Get-command gives us a list of all the cmdlets available in PowerShell along with Aliases & Functions. To learn more about the Get-Command cmdlet, we are going to ask the mentor by executing the cmdlets:
# To Lean more about the Get-Command
As we learn about the new cmdlets, we need to find out more options available with a particular command, and Get-Member, will help us to find all the properties & methods available with a command. For example, if I want to know all the methods & Properties available with the Get-Command, I will execute the following cmdlets:
# Using Get-Member
Get-Command | Get-Member
Although I said three cmdlets, this one will be useful, when you execute a script. PowerShell is secure by default; it will not allow you to run any script, unless, you said it to do so. The default execution policy (or script execution policy) is Restricted; however, you can change it. You can set your execution policy as Remote Signed, although, later you will learn that the best possible option should be AllSigned. To learn more about all the available option or set it to remote signed , ask your mentor, as shown below:
# To verify the current execution policy of the system
# To lean all about the Set-ExecutionPolicy
Get-Help Set-ExecutionPolicy -Full
# To Set the execution policy to RemoteSigned
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Now I’m more Confused
In case I confused you (sometimes, I do confuse myself), here are two superb courses on PowerShell on Microsoft Virtual Academy, presented by, none other than, the inventor of PowerShell: “Jeffery Snover” (b|t), Distinguished Engineer & Lead Architect for Windows Server Division.
- Getting Started With PowerShell 3.0 JumpStart
- Advanced Tools & Scripting with PowerShell 3.0 JumpStart
A few other online resources, which you may want to visit:
1. Windows PowerShell Blog
2. Hey, Scripting Guy! Blog
3. PowerShell Magazine
4. Ravikanth’s Blog
5. Bangalore PowerShell User Group
Hope, you will start using PowerShell, Happy Learning