A few PowerShell tips – Part 2 – Useful commands / switches.

Few days ago I wrote similar article:

Part 1 : https://paweljarosz.wordpress.com/2016/02/21/a-few-powershell-tips-part-1-the-one-about-help/

only_lowe

Here is another portion of PowerShell features I find worth remembering, and I tend to forget about. Actually this is the reason why all these articles are here ^^

 

Show-Command – GUI to build PowerShell commands

 

Show-Command: That feature came out in PowerShell 3.0. Gives great ability to create commands in situation when you are not feeling so well with the shell yet, or if you simply would like to check all the attributes of a certain command:

PowerShell_Tips1

We can see that there is possibility to set even things like pipeline variable (that particular appeared in PowerShell 4.0 – here is a great article about it: https://rkeithhill.wordpress.com/2013/07/20/powershell-v4-pipelinevariable-common-parameter/) and many others through the GUI.

 

How to check PowerShell version installed

 

By the way, to check PowerShell version commands “host” or “$PSVersionTable” can be used:

PowerShell_Tips2

 

Simply “one-liners” simpler with version 3.0

 

Starting from version 3.0 there there is a possibility to use simplified syntax (but only for simple commands like single property listing etc.), for example in PowerShell 2.0 simply Get-Mailbox would look like:

Get-Mailbox | ? {$_.name -match “test”}

And other, shorter, way we can do the same in PowerShell 3.0:

Get-Mailbox | ? name -match test

PowerShell_Tips3

So there is no need for “$_.”, braces and quotes 🙂

 

Be lazy and set default parameters for cmdlets 🙂

 

Another worth mentioning thing is $PSDefaultParameterValues. Variable is set per session, and can be overwritten. Example usage:

$PSDefaultParameterValues = @{“get-wmiobject:class”=”win32_bios”}

After that when running gwmi cmdlet we wil by default will get win32_bios class:

PowerShell_Tips4

 

 -expandproperty – but why?

 

Exactly!

giphy1.gif

If you haven’t used that yet, you might be asking, why it is a cool thing to use. Here is a good example. You plan to use Get-WmiObject cmdlet that is not accepting pipelining – why? Because yo udo not want to use Get-CimInstance that accepts pipelining and does same…  🙂 Anyway after trying to read Win32_BIOS class you might get:

expand1.JPG

The exit from that situation is to get computer name, and puke it out from cmdlet as string – not an object:

expand2.JPG

expand.JPG

 

Label and expression – custom naming properties

 

This one is more useful you might expect 🙂 Great example can be found here:

https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276

where Jeff and Jason are explaining how to understand usage of pipe.

Here is also great article about it:

https://blogs.technet.microsoft.com/heyscriptingguy/2013/03/25/learn-about-using-powershell-value-binding-by-property-name/

Ed tells there that we can run a pipeline using property “by value” and “by name”, but there is a problem while we have a different name. And here somes the help! Example:

Let’s say we got one workstation and we would like to read status of particular service form it. If we run it Get-ADComputer and we run a simply pipe on Get-Service – we will ger an error:

GetServiceGetAdComputerNameFailed

BUT WHY!?!?!?!!?

whydawsons.JPG

Hey sure! Telling you now. So what we need to check is: Do we have something, which can let us to connect these two cmdlets to each other?

Firstly let’s check Get-ADComputer:

getAdComputer

Property is with the computer name is “Name”. Now we need to get state of one service on that workstation, we check if Get-Service if there is a change to get hookup:

Get-ServiceProperty

So what we need to do here is…:

GetServiceGetAdComputerName

…and TAAADAAAAM!! All we needed to do is to change the property name.

“n” is alias “name”, you can also use “l” for label in exchagne. “e” is for expression. So that part of a command might look like:

select @{name=’ComputerName’;expression={$_.name}}

or

select @{label=’ComputerName’;expression={$_.name}}

 

-PassThru the command

 

You want to run a command and see a right away output of what you did? No problem, just use “-PassThru” switch!

PassThru.JPG

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s