Where’s the value

PowerShell is object based – its the one fact that is mentioned in very introduction to PowerShell – and its the use of objects that gives PowerShell its reach and power.

The use of objects has one tiny drawback. When you want the actual value of a property you have to do slightly more work.

This is representative of a typical PowerShell expression

£> Get-Process powershell | select Path


Now if you want the value of path itself to use elsewhere you have to do one of three things:

£> $proc = Get-Process powershell
£> $proc.Path


£> $path = Get-Process powershell | select -ExpandProperty Path
£> $path


£> $proc = (Get-Process powershell | select Path).path
£> $path

The first one gives you an object to work with.  If you want other properties you can access them later.

The other two options give you a variable containing just the information you need – the value of the Path property.

Which one works best  – depends on what you are doing.  I use all three techniques on  a regular basis though tend to prefer using option 2 to option 3 – mainly because I find it easier to read when I come back to the script weeks, months or even years later.

This entry was posted in Powershell Basics. Bookmark the permalink.

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