PowerShell How to use -and in Where-Object - powershell

I want to filter a command result using two conditions. Here are my commands
$list=Get-PnpDevice | Sort-Object -Property Name | Where-Object -Property ConfigurationFlags -NotLike '*DISABLED*' | ft Name, InstanceId -AutoSize
and the next filter is
$list=Get-PnpDevice | Sort-Object -Property Name | Where-Object -Property FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize
both of them works separately but I want to join them using and command. I tried to use -AND as following command but it keeps raising errors
Get-PnpDevice | Sort-Object -Property Name | Where-Object{
( ConfigurationFlags -NotLike '*DISABLED*') -and
( FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize

Simply use the The $_ automatic variable in your Where-Object to reference the property names:
Get-PnpDevice | Sort-Object -Property Name | Where-Object{
( $_.ConfigurationFlags -NotLike '*DISABLED*') -and
( $_.FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize

You can pipe 'Where' clauses together... it's simpler syntax and easier to read
Get-PnpDevice | Sort-Object -Property Name | Where ConfigurationFlags -NotLike '*DISABLED*' | Where FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize

Related

How do I get the server name in Format-Table?

This is probably a version issue, but I simply need to get the server name into the Format-Table PowerShell command.
$compArray = Get-Content C:\Users\Me\Documents\ServerList_All.txt
$Proc = foreach ($strComputer in $compArray) {
Get-WMIObject Win32_Service | Where-Object {
$_.Name -like 'SQL*' -or
$_.Name -like 'MSSQL*' -or
$_.Name -like 'OLAP*' -or
$_.Name -like 'MSDTS*' -or
$_.Name -like 'MSOLAP*' -or
$_.Name -like 'ReportServer*'
} | Sort-Object -Property Name | Format-Table $strComputer, Name, State
}
$Proc | Out-File C:\Users\ME\Documents\ServerStatus_All.txt
This works in PS v2:
| Sort-Object -Property Name | Format-Table Name, State
This does not, but does work in PS v3:
| Sort-Object -Property Name | Format-Table $strComputer, Name, State
Error:
Format-Table : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
The only difference is the $strComputer variable. I am reading from a text file, and everything is beautiful in v3+.
No, I cannot upgrade to a newer PS version on the server I am running this from, sadly.
This should work for you in both...
$compArray = (Get-ADComputer -Filter *).Name
$Proc = foreach ($strComputer in $compArray) {
Get-WMIObject -Class Win32_Service -ComputerName $strComputer |
Where-Object {
$_.Name -like 'SQL*' -or
$_.Name -like 'MSSQL*' -or
$_.Name -like 'OLAP*' -or
$_.Name -like 'MSDTS*' -or
$_.Name -like 'MSOLAP*' -or
$_.Name -like 'ReportServer*'
} |
Select-Object -Property #{Name = 'Computer';Expression={$strComputer}}, Name, State |
Sort-Object -Property Name |
Format-Table -AutoSize
}
$Proc
# Results
Computer Name State
-------- ---- -----
LABSQL01 MSSQLFDLauncher Running
LABSQL01 MSSQLSERVER Running
LABSQL01 SQLBrowser Stopped
LABSQL01 SQLSERVERAGENT Running
LABSQL01 SQLTELEMETRY Running
LABSQL01 SQLWriter Running
Computer Name State
-------- ---- -----
LABWSM01 MSSQL$MICROSOFT##WID Stopped

How to access in this array of objects

I have this part of code -
$result = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, Publisher, InstallDate, UnistallString |
Where-Object InstallDate -GT 20180201 |
Where-Object UnistallDate -NotMatch " " |
Sort-Object -Property InstallDate -Descending |
Format-Table –AutoSize
$result
With the result of this command I get an array of objects, but if I try to access in it I get no result.
Example:
$result.UnisistallString. How can I access in it to get only the attributes of that parameter? Because with that then I need to print on video the name of the program and the unistall path.
You should not use the Format-Table cmdlet if you need to access the data in your code later. Also you have a typo in your example and the select statement. This should work:
$result = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, Publisher, InstallDate, UninstallString |
Where-Object InstallDate -GT 20180201 |
Sort-Object -Property InstallDate -Descending
Now Access it using:
$result.UninstallString

How to set Get-ADComputer result as object

I have this Powershell command:
Get-ADComputer -filter { Name -like 'srv*' } | Select -Expand dnshostname | Export-CSV -path ad_export.csv
In the CSV it only writes the length of the Strings. I read that I have to pipe an object to Export-CSV so it writes the Servernames and not only the length. How do I do that?
Based on the requirement , you can use :
Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select -Expand DNSHostName | Export-CSV -path ad_export.csv
# Getting just the hostname
Get-ADComputer -Filter * | Select -Expand Name | Export-CSV -path ad_export.csv
# Getting a specific computer
Get-ADComputer -Filter { Name -eq 'server2012' } -Propert LastLogonTimestamp | Select DistinguishedName, LastLogonTimestamp | Format-Table -AutoSize | Export-CSV -path ad_export.csv
Note: You can use the "where" clause also if required.
Hope This suffice your need

where-object in powershell

How can I club these two ?
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*|`
Where-Object {$_.displayname -like "*Database Engine Services*" } | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |Format-Table -AutoSize
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Where-Object {($_.displayname -like "*Engagement*") } |`
Format-Table –AutoSize
I went with a RegEx approach to condensing your Where-Object filtering to a single instance, but you could also use -OR to also bring it into a single instance. Also, there is no need to do a Select-Object into a Format-Table as just using Format-Table will let you specify what properties to display.
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {
$_.DisplayName -match '^Database Engine Services|Engagement'
} | Format-Table DisplayName, DisplayVersion, Publisher, InstallDate -AutoSize
Here is the alternative using -OR with your -LIKE statement:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {
$_.DisplayName -LIKE 'Database Engine Services*' -OR $_.DisplayName -LIKE '*Engagement*'
} | Format-Table DisplayName, DisplayVersion, Publisher, InstallDate -AutoSize

How to filter multiple items from regerstry by using whrere-object -like?

I am trying to get few items from registry by using where-object, I can only have one item filtered but multiple items...is there anything wrong with my script?
This code works fine for only one item
Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ |
ForEach-Object {Get-ItemProperty $_.pspath} |
Where-Object {
$_.Displayname -like 'adobe air'
} |
Select-Object DisplayName,DisplayVersion |
Sort-Object DisplayName |
Out-GridView
But if i set it to filter multiple items, it runs, ends, without any result... any idea why?
Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ |
ForEach-Object {Get-ItemProperty $_.pspath} |
Where-Object {
$_.Displayname -like 'adobe air' -and
$_.Displayname -like 'Java*' -and
$_.Displayname -like 'TeamViewer*'
} |
Select-Object DisplayName,DisplayVersion |
Sort-Object DisplayName |
Out-GridView
If I use
Where-Object {
$_.Displayname -like "Security*"
}
It only gives me 3 items matches Security, not all of them, Why?
The issue is the logic you put in. -and means both need to be true, use -or instead.