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
Related
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
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
I need to get a list of locally installed software from a PC.
I'm using the following piece of code for this :
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table -AutoSize > app32.txt
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table -AutoSize > app64.txt
Then I need a single list of installed software sorted by DisplayName column.
Trying to use:
cat apps*.txt |
where {$_ -ne ""} |
Sort-Object -Property #{Expression="DisplayName"} |
sc all_apps.txt
But in fact the list is not sorted in alphabetic order at column DisplayName and also the result file contains empty strings which should not be included
Don't use Format-Table like that. Format-Table is for presenting information to the user, not presenting information to other commands. It turns the output into a string instead of objects that PowerShell can manipulate.
Try:
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-Csv -Path app32.csv -NoTypeInformation
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-Csv -Path app64.csv -NoTypeInformation
Import-Csv app32.csv, app64.csv |
Where-Object { -not [System.String]::IsNullOrWhitespace($_.DisplayName) } |
Sort-Object -Property DisplayName |
Export-Csv -Path all_apps.csv -NoTypeInformation
Or, alternately:
Get-ItemProperty -Path HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object { -not [System.String]::IsNullOrWhitespace($_.DisplayName) } |
Sort-Object -Property DisplayName |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-Csv -Path app64.csv -NoTypeInformation
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | export-csv app32.csv -NoTypeInformation
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | export-csv app64.csv -NoTypeInformation
$softwarelist = import-csv -Path ".\app32.csv"
$softwarelist += import-csv -path ".\app64.csv"
$Softwarelist | Sort-Object -Property DisplayName
Basically I used export-csv instead of just writing it to a txt file, this allows powershell to use import-csv and when it brings it in it recognises the different properties; when it gets the content from a .txt it just reads in a big ol' string.
Using += we can add both files together and then just sort-object on them to display them how you like.
Edit: I should add you could skip writing them out as a CSV and instead just store them in a variable and use that. Something like:
$softwarelist = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$softwarelist += Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$Softwarelist | Sort-Object -Property DisplayName
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.
I want to get the Display Name, Name, Start Mode, Start Name, and State of all Automatic services that are in a Stopped state on a Windows server. Normally, I would just do
get-wmiobject -class win32_service | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"} | select DisplayName, Name, StartMode, StartName, State
However, the cmdlet above does not distinguish between a state of "Automatic" and "Automatic Delayed Start". The cmdlet I have below will not report any services that have a state of auto delayed start, but I don't know how to get it to also display the other properties I need.
(Get-WmiObject -Class Win32_Service -Filter "state = 'stopped' and startmode = 'auto'" | Select-Object -ExpandProperty name) | Where-Object {(Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services | Where-Object {$_.property -contains "DelayedAutoStart"} | Select-Object -ExpandProperty PSChildName) -notcontains $_} | Select-Object #{l='Service Name';e={$_}}
How can I modify the above cmdlet so that it will also display the other properties I want?
EDIT:
I know the method below will work, but its inefficient and non-powershell-like.
$auto_services = #((get-wmiobject -class win32_service -filter "state='stopped' and startmode='auto'" | select-object -expandproperty name) | ? {(get-childitem HKLM:\SYSTEM\CurrentControlSet\Services | ? {$_.property -contains "DelayedAutoStart"} | Select-Object -ExpandProperty PSChildName) -notcontains $_})
foreach ($service in $auto_services) { Get-WMIobject -class win32_service | ? {$_.Name -eq $service} | Select DisplayName, name, startmode, startname, state}
EDIT 2:
What would be even better is if you could list all services and the desired properties and somehow make it so that the "Automatic Delayed Start" services actually show "Auto Delayed Start" as the StartMode instead of showing just "Auto".
Using PowerShell 4.0 (with -PipelineVariable) you can do the following :
get-wmiobject -class win32_service -PipelineVariable s | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| where {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart -eq 1} | % {select -InputObject $s -Property DisplayName, Name, StartMode, StartName, State}
Using previous versions you should assign the service object to a var during the pipeline.
get-wmiobject -class win32_service | % {$s=$_;$s} | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| where {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart -eq 1} | % {select -InputObject $s -Property DisplayName, Name, StartMode, StartName, State}
Edited
Here is a version for PowerShell 2.0 of all stopped services that are in "Automatic" startmode and not not "Automatic Delayed Start" :
get-wmiobject -class win32_service | % {$s=$_;$s} | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| % {$d = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart;$_ } | where {$d -ne '1'} |% {select -InputObject $s -Property #{name="DelayedAutoStart";expression={$d}},DisplayName, Name, StartMode, StartName, State}