Using -ExcludeProperty in Select-Object

There are some properties that I just don’t want to see…

Words: 394

Time to read: ~ 2 minutes

Update 2019-07-24: Newer blog post here : https://nocolumnname.blog/2019/07/24/excludeproperty-in-powershell-core/

Nice, short, simple blog post today.

Head, Meet Wall

Working on getting data file information from the SQL Server SMO objects:

<br>
foreach ($DataFile in $db.FileGroups.Files) {</p>
<p>    $DataFile</p>
<p>}<br>

 

Original No Pipe
ExecutionManager = ExecutionManager…that’s helpful!

Hmm, Properties, ExecutionManager, and URN (hidden from view) are not needed. That is not a problem, I’ll just pipe them to Select-Object and include them in the -ExcludeProperty parameter.

Head, Meet Keyboard

So we change our code to the following:

</p>
<p>foreach ($DataFile in $db.FileGroups.Files) {</p>
<p>$DataFile |<br>
    Select-Object -ExcludeProperty Urn,<br>
                                   Properties,<br>
                                   ExecutionManager</p>
<p>}<br>

Original SelectObject NoChange
These suck at Hide ‘n Seek…

What the…? I excluded these guys! Do they not know the meaning of “Exclude”?!

Shh…Calm Yourself

As Kevin Feasel ( blog | twitter | CuratedSQL ) once said “Get-Member early and Get-Member often“, I’d like to add to that:

Get-Member early and Get-Member often…and don’t forget to Get-Help!

<br>
help Select-Object -ShowWindow<br>

GetHelp
-ShowWindow cause it feels fancy!

Now I’m interested in the -ExcludeProperty parameter but I read the full thing, it’s not that big anyway. Thankfully the help, well, helps me.

Help ExcludeProperty
Gotta have properties to exclude them I guess…

Main point here is:

This parameter is effective only when the command also includes the Property parameter.

Bruce Banner, Not HULK.

I change up my code to include the  -Property * and see how it works…

<br>
foreach ($DataFile in $db.FileGroups.Files) {</p>
<p>    $DataFile |<br>
        Select-Object -Property * -ExcludeProperty Urn,<br>
                                                   Properties,<br>
                                                   ExecutionManager</p>
<p>}<br>

ItWorks
mwahahah I’m the greatest!!! eh I mean…yes, that’ll do!

Clear-Host

Now, there is nothing here that I would consider a ground-breaking, earth-shattering revelation. I had a problem, resolved it, and decided to share it.

  • Time to fix: ~3 minutes.
  • Time to write: ~7 minutes.
  • Overall time taken: ~10minutes.

This is quite possibly the shortest post I’ve ever written and there’s a reason for that; #SQLNewBlogger.

Blog posts don’t have to have these big revelations for you. It seems to help, yeah, but that’s mainly because if you feel passionate about something, it comes across in your writing. Besides, what you consider trivial, other people may never have thought of or encountered before. So go on! Give it a go!

Got a problem, write it up.
Got an opinion, voice it.
Got a script, share it.

Got it? Good!

 

Author: Shane O'Neill

DBA, T-SQL and PowerShell admirer, Food, Coffee, Whiskey (not necessarily in that order)...

6 thoughts on “Using -ExcludeProperty in Select-Object”

  1. Great Article. I really enjoy your writing style and it the article kept me from getting too angry about it. “Shhh … Calm yourself”. Oh such great advice. Long story short – Thanks!

Leave a Reply

Discover more from No Column Name

Subscribe now to keep reading and get access to the full archive.

Continue reading