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>

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>

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>

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.

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>

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!
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!
helped me ! Thanks
cmdlet | Select-Object * -ExcludeProperty prop1,prp2.prop3 is the way…
Yup, thank you. Figured that out in the blog post.