Powershell Script to output Installed Programs - powershell

I'd like to do this using the win32_product wmi class.
I need the script to simply count the number of products installed and also output the time taken to execute the script.
What I have atm doesn't seem to work correctly:
$count = 0
$products = get-wmiobject -class "Win32_Product"
foreach ($product in $products) {
if ($product.InstallState -eq 5) {
count++
}
}
write-host count

Beware! Using WMI's Win32_Product class, which the question and the previous 2 answers do, is not advised for this purpose.
In a nutshell: using Win32_Product is not an innocuous query because it has side effects. Quoting Microsoft, "[It]... initiates a consistency check of packages installed, verifying and repairing the install." (emphasis mine)
References:
Microsoft Support Reference
Use PowerShell to Quickly Find Installed Software (Hey, Scripting Guy! blog)
Win32_Product WMI Class Replacement
Why Win32_Product is Bad News!
So what is a better (safer) solution?
Marc Carter, writing the guest column in the Hey, Scripting Guy! blog above takes the first volley, providing a custom PowerShell function, back on my system it returned only half as many entries as the Win32_Product invocation. Also, it is a lot of code (3 dozen lines or so). In the comments to his post, however, knutkj offers this much shorter version that does the same thing:
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Sort-Object -Property DisplayName |
Select-Object -Property DisplayName, DisplayVersion, InstallLocation
But it does, as I said, the same thing: not provide a full list. But it is a start.
Later in the comments Nick W reported that there are actually 3 registry paths of interest, though not all may be present on every system. Further, when looking at those 3 paths, one has to do some additional filtering.
Combining both of those, adding in a few more output fields, and making the code safe to run in strict mode, I arrived at this simple solution:
function Get-InstalledPrograms()
{
$regLocations = (
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\",
"HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
)
Get-ChildItem ($regLocations | Where { Test-Path $_ } ) |
Get-ItemProperty |
Where {
( (Get-Member -InputObject $_ -Name DisplayName) -and $_.DisplayName -ne $Null) -and
(!(Get-Member -InputObject $_ -Name SystemComponent) -or $_.SystemComponent -ne "1") -and
(!(Get-Member -InputObject $_ -Name ParentKeyName) -or $_.ParentKeyName -eq $Null)
} |
Sort DisplayName |
Select DisplayName, DisplayVersion, Publisher, InstallLocation, InstallDate, URLInfoAbout
}

A bit late on this but the "more powershellish way":
$(Get-WmiObject -Class "Win32_Product" -Filter "InstallState=5").Count

Roman Kuzmin is right about the typo. Correction will solve almost everything.
To make it more powershellish, I would use
get-wmiobject -class "Win32_Product" |
? { $_.InstallState -eq 5 } |
measure-object |
select -exp Count
And considering the time, you can wrap it into measure-command
measure-command {
$count = get-wmiobject -class "Win32_Product" |
? { $_.InstallState -eq 5 } |
measure-object |
select -exp Count
write-host Count: $count
}

Related

Out-gridview does not sorting results

I have a script that I am trying to collect drive letters from a list of servers (as well as used space and free space) and then gridview the results out.
$servers = Get-Content "path.txt"
foreach ($server in $servers) {
Invoke-Command -ComputerName $server {Get-PSDrive | Where {$_.Free -gt 0}}
Select-Object -InputObject usedspace,freespace,root,pscomputername |
Sort-Object root -Descending | Out-Gridview
}
I can get it to display the drive information for each server on the list but gridview does not work. I have tried moving the brackets around (before and after gridview) as well as piping elements but have had no luck.
Can anyone advise me as to what I am doing wrong? I feel like it is something simple but all of the examples I am finding online do not use the foreach command which I think has to do with throwing it off.
Your Select-Object is missing pipeline input - pipe the Invoke-Command call's output to it.
Instead of -InputObject, use -Property:
Note: -InputObject is the parameter that facilitates pipeline input, and is usually not meant to be used directly.
As with Sort-Object, -Property is the first positional parameter, so you may omit -Property in the call below.
foreach ($server in Get-Content "path.txt") {
Invoke-Command -ComputerName $server { Get-PSDrive | Where { $_.Free -gt 0 } } |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
}
Also note that -ComputerName can accept an array of computer names, which are then queried in parallel, so if you want to query all computers and then call Out-GridView only once, for the results from all targeted computers:
Invoke-Command -ComputerName (Get-Content "path.txt") {
Get-PSDrive | Where Free -gt 0
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
To group the results by target computer, use
Sort-Object pscomputername, root -Descending
If you'd rather stick with your sequential, target-one-server-at-a-time approach, change from a foreach statement - which cannot be used directly as pipeline input - to a ForEach-Object call, which allows you to pipe to a single Out-GridView call:
Get-Content "path.txt" |
ForEach-Object {
Invoke-Command -ComputerName $_ { Get-PSDrive | Where Free -gt 0 }
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview

Powershell command Enable-ComputerRestore not accepting variable

Can someone help me with this powershell script?
I can't get it to work.
$drives = Get-WmiObject -class win32_logicaldisk |
Where-Object { $_.DriveType -eq 3 }
$drives | ForEach-Object { Enable-ComputerRestore $_.Name }
Moreover the following works like expected!
$drives = Get-WmiObject -class win32_logicaldisk |
Where-Object { $_.DriveType -eq 3 }
$drives | ForEach-Object { Write-Host $_.Name }
Thanks in advance.
Enable-ComputerRestore seems to have two unusual requirements:
The (positionally implied) -Drive argument(s) must end in \ (backslash), e.g, C:\
In order to target a drive other than the system drive, the system drive must also be specified, alongside the non-system drive or System Restore must already be turned on for the system drive.
Since your code enumerates all local disks (.DriveType -eq 3), the system drive is by definition among them, so the simplest solution is to pass all local drives at once to Enable-ComputerRestore, as an array:
$driveSpecs =
Get-CimInstance -Class Win32_LogicalDisk |
Where-Object { $_.DriveType -eq 3 } |
ForEach-Object { $_.Name + '\' }
Enable-ComputerRestore $driveSpecs
As an aside:
I've replaced Get-WmiObject with Get-CimInstance, because the CIM cmdlets superseded the WMI cmdlets in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell Core (version 6 and above), where all future effort will go, doesn't even have them anymore. For more information, see this answer.
A more concise and more efficient way to filter the Get-CimInstance output is to use a -Filter argument in lieu of a separate Where-Object call:
Get-CimInstance -Class Win32_LogicalDisk -Filter 'DriveType = 3'
Found the solution by my self. Posting for others.
$OSDriveLetter = (Get-ComputerInfo | Select-Object -Property "OsSystemDrive").OsSystemDrive
Get-CimInstance -class win32_logicaldisk |
Where-Object { $_.DriveType -eq 3 } |
ForEach-Object { Enable-ComputerRestore "$($_.Name + '\')", "$($OSDriveLetter + '\')" }

Powershell IF and not condition not working properly

I'm struggling to have two conditions met, somehow it seems only one of them works. I'm trying to do :
If a user is connected AND NOT at the lock screen, ask for permission. The commands themselves have been verified and work individually but I must be missing something. Here is what i have:
if (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem | Select-Object -expandproperty UserName -and -not (get-process -ComputerName $poste -name logonui)) {
"ask for permission"
}
Right now it just doesn't go in this code, it skips to the lower part where something else is happening.
What is wrong with my syntax ?
I can work around it and make it work this old fashioned way from my CMD days:
Clear-Variable -name statut_user
$statut_user -eq 0
if (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem | Select-Object -expandproperty UserName) {$statut_user +=1}
if (-not (get-process -ComputerName $poste -name logonui)) {$statut_user += 1}
if ($statut_user -eq 2) {
"ask for permission"
}
It works, but not as clean as a proper one liner with the two conditions. Thank you for your help!
ANSWER EDIT: Thanks to vonPryz's answer below i ended up using :
$utilisateur = Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem | Select-Object -expandproperty UserName
$ecran_verr = get-process -ComputerName $poste -name logonui
if( -not ($ecran_verr) -and ($utilisateur)) {
"ask for permission"
}
Aim for clarity, not small codebase size. Instead of making WMI calls in the if statement and piping the results, consider something more readable. Like so,
$cs = gwmi -computername $p Win32_ComputerSystem
$uname = $cs | select-object -expandproperty UserName
$logonui = get-process -ComputerName $p -name logonui
if( -not ($logonui) -and ($uname )) {
# Stuff
}
This approach makes it easy to check that the WMI objects contain sensible values, whatever those may be. Then it should be easier to write a concise conditional statement.
While breaking an expression down into multiple steps is always a good idea for debugging, as demonstrated in vonPryz's helpful answer, sometimes you do want the concision of a single expression without auxiliary variables.
What is wrong with my syntax?
You're missing (...) around the Get-WmiObject ... | Select-Object ... pipeline.
To use a command or pipeline as part of a larger expression, you must always enclose it in (...) A command in PowerShell is a call to an executable - be it a cmdlet, function, alias, or external program.
A simple example:
# !! BROKEN: tokens `-eq 'jdoe'` are interpreted as *arguments for Select-Object*
# !! rather than as operator -eq and RHS 'jdoe'
if (Get-WmiObject Win32_ComputerSystem | Select-Object -Expand UserName -ne 'jdoe') {
'not jdoe'
}
# OK: (...) around the pipeline properly embeds it in the overall expression:
if ((Get-WmiObject Win32_ComputerSystem | Select-Object -Expand UserName) -ne 'jdoe') {
'not jdoe'
}
Here's a fixed version of your original command that fixes that improves other aspects too:
if (
(Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName `
-and -not `
(Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
) {
"ask for permission"
}
Given that your Get-WmiObject call only ever outputs 1 object, you can access the .UserName property directly, which is also more efficient than piping to Select-Object -ExpandProperty.
Get-Process outputs a non-terminating error if a process by the given name cannot be found, so -ErrorAction SilentlyContinue suppresses that.
Note the use of ` as a line-continuation character, which allows spreading the conditional across multiple lines, making it much more readable.
Note that the ` must be at the very end of the line.

Remove column name from PowerShell select-object results

Overall my goal is to get the VNC version for a list of remote computers along with the uninstall GUID so I can remotely uninstall VNC Viewer from certain computers. I have used the Get-WmiObject -Class Win32_Product but that is extremely slow.
I have the following script but in the results it includes the name of the select-object parameter.
$computers = Get-Content -Path "C:\Computers.txt"
$Results = #()
ForEach ($Computer in $Computers) {
$Results += New-Object PSObject -Property #{
"ComputerName" = $Computer
"Name" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
| Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object DisplayName
"DisplayVersion" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
| Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object DisplayVersion
"ModifyPath" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
| Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object ModifyPath
"Vendor" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
| Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object Publisher
}
}
$Results | Select-Object ComputerName,Name,DisplayVersion,ModifyPath,Vendor | Sort-Object ComputerName | Export-Csv C:\VNC.csv -notype ;
My results look like this:
ComputerName : ComputerName
Name : #{DisplayName=VNC Viewer 5.2.3}
DisplayVersion : #{DisplayVersion=5.2.3}
ModifyPath : #{ModifyPath=MsiExec.exe /I{18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}}
Vendor : #{Publisher=RealVNC Ltd}
I would want it to look like this:
ComputerName : ComputerName
Name : VNC Viewer 5.2.3
DisplayVersion : 5.2.3
ModifyPath : MsiExec.exe /I{18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}
Vendor : RealVNC Ltd
Is this possible or am I going about this script entirely wrong? I haven't figured out a way to run this Invoke-Command for multiple parameters and still output the results in individual columns any other way.
This script works but takes forever for 100's of computers:
if (Test-Path C:\VNCInstalled.csv) {Remove-Item C:\VNCInstalled.csv}
if (Test-Path C:\Computers.txt) {Remove-Item C:\Computers.txt}
$DirSearcher = New-Object System.DirectoryServices.DirectorySearcher([adsi]'')
$DirSearcher.Filter = '(&(objectClass=Computer)(!(cn=*esx*)) (!(cn=*slng*)) (!(cn=*dcen*)) )'
$DirSearcher.FindAll().GetEnumerator() | sort-object { $_.Properties.name } `
| ForEach-Object { $_.Properties.name }`
| Out-File -FilePath C:\Computers.txt
Get-Content -Path c:\Computers.txt `
| ForEach-Object {Get-WmiObject -Class Win32_Product -ComputerName $_} `
| Where-Object -FilterScript {$_.Name -like "VNC V*"} `
| select-object #{Name="ComputerName";Expression={$_.PSComputerName}},
Name,
#{Name="InstallLocation";Expression={$_.PackageCache}},
Vendor,
Version,
#{Name="GUID";Expression={$_.IdentifyingNumber}} `
| Sort-Object ComputerName `
| Export-CSV -path c:\VNCInstalled.csv -notype
Change all of your Select-Object commands to Select-Object
-ExpandProperty PropertyName, to discard the property name / column header.
This is the answer I gave three years ago and I think it was really a poor answer. Let me do a better job now.
Why your current code is slow
Your current code enumerates all machines from AD and puts them in a file called Computers.txt. Simple, and you do it fine.
Next up, your code performs this operation:
Get-Content -Path c:\Computers.txt |
ForEach-Object {Get-WmiObject -Class Win32_Product -ComputerName $_} `
| Where-Object -FilterScript {$_.Name -like "VNC V*"} [...]
This can be summarized as 'For each computer, request the full Win32_product table, and then after that, filter down to apps named VNC.' This is HUGELY performance impacting and for a few reasons.
Even on a fast modern computer, querying Win32_Product will take 30 seconds or more, because it returns every application installed. (on a new VM for me it took more than a minute with just a handful of apps installed!)
Querying Win32_Product also has this fun quirk which makes it take even longer, quoted from MSDN Documentation on the Win32_Product Class
Warning Win32_Product is not query optimized. Queries such as "select * from Win32_Product where (name like 'Sniffer%')" require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure. For more information, see KB Article 794524.
So to summarize, querying Win32_Product is slow, AND it also triggers a consistency chceck on every app, AND we also have this query written to retrieve every single app before filtering. These add up to a process which probably takes ~3 minutes per pc, and will operate serially (one after the other) and take forever.
How to fix it
Software info can be retrieved reliably in two places:
If you have SCCM/ConfigMgr installed on your devices, it adds the Win32_AddRemoveProgram WMI Class you can query, which is a super fast version of Win32_Product
If not, we can always retrieve info from the registry.
Here's a short snippet to get applications like VLC installed on a computer (I don't have VNC like you, so I'm making due)
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-object DisplayName -like "VLC*" |Select-Object DisplayName, DisplayVersion, Publisher, InstallDate,UninstallString
DisplayName : VLC media player
DisplayVersion : 3.0.8
Publisher : VideoLAN
InstallDate :
UninstallString : "C:\Program Files (x86)\VideoLAN\VLC\uninstall.exe"
This operation is much faster, only 400 MS or so. Sadly we cannot get much faster using the registry as it has a very weird PowerShell provider that doesn't implement the -Filter parameter, so we do have to retrieve all programs and then filter to our desired choice.
Updating your script to use this function instead
I took the liberty of rewriting your script to use this approach, and restructured it a bit for better readability.
$results = New-object System.Collections.ArrayList
$computers = Get-Content -Path c:\Computers.txt
foreach ($computer in $computers){
#get VNC entries from remote computers registry
$VNCKeys = Invoke-Command -ComputerName $computer -ScriptBlock {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-object DisplayName -like "VNC V*" |
Select-Object DisplayName, DisplayVersion, Publisher, UninstallString, #{Name=‘ComputerName‘;Expression={$computer}}
}#end of remote command
if ($VNCKeys -ne $null){
forEach($VNCKey in $VNCKeys){
[void]$results.Add($VNCKey)
}
}
}
$results | Sort-Object ComputerName | Export-CSV -path c:\VNCInstalled.csv -NoTypeInformation

PowerShell Where-Object $_.name -like -in $list

I am new to PowerShell and ran into a bit of a roadblock. I am trying to pull program name and version information from multiple servers.
I have a list of the program names in a $list variable, but the program names also contain the version numbers in them. I am just storing the names of the programs in the list variable without the version numbers.
I am trying to figure out a way to use both the -like and -in parameters with the Where-Object cmdlet in order to match the full program entry name (e.g. AdToUserCacheSync 1.10.1.10) with my entry in the $list variable (e.g. AdToUserCacheSync).
How can I do this?
$list = Get-Content "\\server\c$\temp\list.txt"
$storeTestServers = Get-Content "\\server\c$\temp\testStores.txt"
foreach ($server in $storeTestServers) {
Get-WmiObject -Class Win32_Product -ComputerName $server |
Select-Object -Property PSComputerName, Name, Version |
Where-Object {$_.pscomputername -like "940*" -and $_.name -like -in "*$list*"}
}
The Where-Object FilterScript block is just a scriptblock that returns $true, $false or nothing - you can do all kinds of crazy things inside it, including looping over an array to see if there is a wildcard match in one of the entries:
Where-Object {
$ProductName = $_.Name
$_.pscomputername -like "940*" -and (
$list | ForEach-Object {
if($ProductName -like "*$_*"){ return $true }
}
)
}
I found the Adobe version by PowerShell:
Get-WmiObject -Class Win32_Product -ComputerName 127.0.0.1 |
Select-Object -Property PSComputerName, Name, Version |
Where-Object {$_.Name -like "Adobe*"} | Out-File Adobe_Log.log