Why does -Exclude "_*" work in 1 circumstance but not another?

Welcome Forums General PowerShell Q&A Why does -Exclude "_*" work in 1 circumstance but not another?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 years ago.

  • Author
    Posts
  • #60099

    Participant
    Points: 0
    Rank: Member

    Hi,

    This is more for my own learning then anything. I'm finding that Get-WMIObject with a -query is faster then passing the whole Get-WMIObject through the pipe and using | Select-Object on it.

    But I'm having a bit of trouble then working with the results.

    My issue is, – this is just some example code. I'm actually querying for specific fields but it still brings back all the "__Name" fields. So trying to do the Select after to pass less across the pipe.

    This works

    Get-WmiObject win32_processor | select * -exclude "_*"

    This doesn't?

    $test = Get-WMIObject -query "SELECT DeviceID, Name, Description, NumberofCores, NumberOfLogicalProcessors, MaxClockSpeed, SocketDesignation, Status FROM Win32_Processor"
    $Test | Select -exclude "_*"

    I thought I had sussed the pipe and variables. But this now calls into question that.

    My end goal is to query WMI, and output the result to a text-file or some other variable to work with, without all the extra gumf.

  • #60100

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    My assumption is that you are trying to exclude the double underscore properties, which are default properties for WMI. Check out this blog that explains what they are and how to get only what you want.

    The -Query is faster because you are only returning those specific properties. You can replicate this by using -Properties:

    Get-WMIObject -Class Win32_Processor -Properties DeviceID, Name, Description, NumberofCores, NumberOfLogicalProcessors, MaxClockSpeed, SocketDesignation, Status
    

The topic ‘Why does -Exclude "_*" work in 1 circumstance but not another?’ is closed to new replies.