DAY-3: Running PowerShell Cmdlets



PowerShell: Running Commands

Powershell, as its name implies, is a shell. It's similar to Cmd.exe command line that you probably used previously. It also has a strong resemblance to the Unix shells, like Bash or even original Unix Bourne shell. Only the difference come with PowerShell commands with all the other shells is that PowerShell has a unique Anatomy of the Cmdlets structure, which is missing in other shells.This structure is very easy to understand and make use while running Cmdlets. Now I will make you understand the anatomy of the Cmdlets in PowerShell.

The Anatomy of PowerShell Command











The figure shows the basic anatomy of a complex PowerShell command. I have tried to use a complex command so that you can all of the different things that might show up. Now we will tell you all few rules for writing PowerShell commands.

1. Cmdlets Naming Convention - Microsoft has established a naming convention for Cmdlets. The rule is this: Names start with a verb like Get, Set, Remove, New and so on. You can run Get-verb to see all the verbs used in PowerShell Cmdlets. After the verb is a dash, followed by singular a noun like Service or Process or Eventlog.

Well, suppose we told that there were cmdlets name New-Service, Get-Service, Get-process, New-process and so forth. Could you guess what command should use for creation of new mailbox or for modifying AD user? If you guessed "New-Mailbox" you have got the first one right. If you guessed Set-User, you were close: it's Set-ADuser. The point is that by having this consistent naming convention with a limited set of verbs, it become possible for you to guess command name, and you could the use Help. along with wildcards, to validate your guess.

2. The first parameter name is -LogName, and it's being given the value Security. Because the value doesn't contain any spaces or punctuation, it doesn't need to be in quotation marks.

3. The Second Parameter name is -Computername, and it's being given two values: WINB and SERVER1. These are in a comma-separated list.

4. The final parameter is -Verbose, and it's a Switch Parameter. That means it doesn't get a value. Specifying the parameter is sufficient.

5.Parameter name always starts with a dash (-). There's mandatory space between parameter name and parameter values and between Cmdlets name and Parameter name. There is no space between dash and parameter name.

6. The best thing about PowerShell, nothing here is case-sensitive.

Aliases: Nicknames for Commands

Although PowerShell commands names can be nice and consistent, they can also be long. A command like Set-WinDefaultInputMethodOverride, there is a lot to type, even with tab completion.

That's where PowerShell aliases come in Play. An Alias name is nothing more than a nickname for a Cmdlet. Tired of typing Get-service? try this








Now run gsv instead of Get-Service, you will observe that the result produced by both the commands is same. When using the alias, the command works in exactly the same way. Alias is just a mask behind the actual Cmdlet. Even if you search help with Alias name, the help system always displays the help for the full command. Try running below command and see the results.

   Get-help GSV

In addition to aliases, which are simply short names for commands, you can also take shortcuts with parameters.Yo have 3 ways to do this,

1. Truncating parameter- PowerShell doesn't force you to type out entire parameter names. Instead of typing -CompuetName, for example, you could go with -comp. The rule is that you have to type enough for the name for PowerShell to be able to disambiguate. If there's a -ComputerName parameter, a -common parameter and a -composite parameter, you 'd have to type at least -Compu, -Comm and -Compo, because that's the minimum number of letters necessary to uniquely identify each.

2. Positional Parameter- In my previous Blog, I had explained how to find which parameter is Positional parameter. For the positional parameter, you don;t need to type the parameter name, you can provide the value in the correct position.

3. Parameter Aliases- Parameters can also have their own aliases, although they can be terribly difficult to find. as they aren't displayed in the help files or anyplace else convenient. For example,



In this case, the output reveals that the -CN is alias name for -ComputerName.

Now I have concluded all the Alias and Shortname concept in one single Cmdlet to show how it works. Now try to identify the Cmdlets name and its parameter, which were provided in Alias form in below screenshot.





Cheating a Bit: Show-Command

Despite our long experience using PowerShell, the complexity of commands syntax can sometimes drive us nuts. One very cool feature of PowerShell is the Show-Command. if you're having trouble in getting command's syntax right, with all the spaces, dashes, commas, quotes, show-command is your friend.

















It lets you specify the command name you're struggling with and then graphically prompts you for the command's parameter. As shown in above Snapshot, each parameter set is on a separate tab, so there's no chance of mixing and matching parameters across sets.

After you finish entering the values for required parameters, show command shows the command and then it executes it in PowerShell console. This is a great way to teach yourself the proper syntax.











Dealing With Errors

It's inevitable that you'll see some ugly red text as you start working with PowerShell, and probably from time to time even after you 're an expert-level shell user. It happens to all of us. But don't let the red text stress you out. PowerShell's error messages are intended to be helpful, as shown in below screenshot, they try to show you exactly where PowerShell ran into trouble.




Error messages Always include the line and char number where PowerShell got confused.

At line:1 char:1

In above example, the error message saying that PowerShell is not able to recognize Get. That's because the command name is wrong, it is supposed to be Get-Comand, not Get Command.

What about the error in below figure? Comment the answer if you able to identify 😊






One of the easiest ways to solve this kind of errors is, first read the help topic for command and second to write the command completely. Sometimes, the error message might not seem helpful and if it seems like you and PowerShell speaking a different language, so you're probably the one in the wrong, and consulting help and spelling out the complete command, parameters and all, is often the quickest way to solve the problem. And don't forget the show-command to try and figure out the correct syntax.

In next blog, we will be covering below listed topics:
1. Pipeline - Connecting commands
2. Exporting data


Thanks for reading my blog. Your valuable suggestions are welcome.

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

Day-5: PowerShell Modules & PSSnapin

DAY-7: POWERSHELL OBJECT'S