Here Are The Three Useful PowerShell Commands
All you need to progress from PowerShell beginner to PowerShell intermediate is mastery of these three straightforward techniques.
You have installed PowerShell, or ‘Turned Windows feature on’, furthermore you have located PowerShell ISE. Incidentally, I suggest you create a shortcut, or better still, Pin this executable to the Taskbar or Start Menu.
In its basic form, any cmdlet returns only limited default information; consequently, the tastiest parameters are often hidden away. Thus, an easy way of extending your repertoire is by simply prefixing Get-Help before the name of the cmdlet you are using.
Let us take Get-Date as example. On its own Get-Date does what you may expect, it returns the current date and time! However, what if want the day of the week, or the time-zone offset?
Get-Help Get-Date -full
# Get-Date -uFormat "%A Time zone %Z"
Note 1: There are two points I always try to remember with Get-Help, firstly, there is no need for the | pipeline, and secondly, I always append -full so that I get examples of the cmdlet in use.
Note 2: Studying Get-Date’s description and examples reveals useful parameters such as -uFormat
Remove the # (Remark) and run the above command once more.
Note 3: The above example is not of great consequence, it’s the principle of calling for Get-Help whenever we are confronted by a new PowerShell cmdlet that’s important. While they won’t admit it, even experienced scripters use Get-Help when tackling new projects. My point is never be too proud to call for Get-Help, trust me, nobody remembers all a cmdlet’s parameters.
Challenge: Investigate each of these commands in turn by prefixing them with Get-Help
Run on its own, without modifiers, Get-Command catalogs all PowerShell’s 300+ cmdlets. When I research real life tasks is that I often have an idea of the command that I am looking for, so take a guess and append the time-honoured asterisk (*). This gives me a list of commands for further investigation.
Imagine the scenario, you want to research the TaskManager, but you are not quite sure of the name of the PowerShell cmdlet.
Get-Command # This gives too much information!
# Try each of these in turn
Get-Command -Noun Process
Get-Command -Verb Stop
Note 4 : You can even apply Get-Help to Get-Command, this how I discovered the -noun and -verb parameters.
Each PowerShell object has more properties and methods than you can ever imagine. To access this ‘secret’ store never miss a chance to pipe a simple cmdlet, or even a complex object into: | Get-Member. I will be surprised if you don’t find at least one property or method that will enhance your original script.
Get-Process | Get-Member
Note 5: Even Get-Member has parameters. If you investigate with Get-Help you will find a useful switch, or to use its correct name, parameter called -MemberType. This is how it filters properties and omits methods from the result.
Get-Process | Get-Member -MemberType property
Note 6: You really do need the pipe (|).
Example of how this research technique may bear fruit. Let us imagine that we are interested in these particular properties: Handles, PriorityClass, or StartTime, then this is how you modify the output of Get-Process.
# Explicit example of adding properties
Get-Process | Format-Table Name, Handles, PriorityClass, StartTime -auto
Note 7: This example employs Format-Table to display the results, and more importantly, to select the best properties to list in the output’s columns.
Challenge: Research with -MemberType, then amend my example so that it displays different properties.
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.
PowerShell’s Golden Rules
PowerShell mimics what you can do in a GUI. The benefit is that experts can issue commands quicker, and with precision, that by clicking through those Windows menus.
- The basic unit is always the verb-noun cmdlet.
- PowerShell’s signature tune is | which is known as the pipeline. The concept is that the output of the first command becomes then input of the second, or third pipeline.
- Employ Get-Member to research methods and properties, which you can refine with pipelining.
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
If you like this page then please share it with your friends
See more Windows PowerShell examples
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.