Check if specific programs are installed on a remote server using powershell - windows

I am writing a script where I need to check specific applications if installed or not on a remote server. I am fairly new to PowerShell, but here is the script I have written
ForEach ($computers in $computer){
Get-WMIObject -Class win32_product -Filter {Name like "%Microsoft%"} -ComputerName $computers -ErrorAction STOP | Select-Object -Property Name,Version | export-csv "C:\progrms.csv"
}
The problem I am facing here is I can get details of only Microsoft application which are installed and if I give multiple names for filter parameter like "Microsoft", "SQL", "app1" so on. This script does not seem to work.
Please let me know what has gone wrong and what needs to be done in order to get the list of specific software's that are installed. Also, note that I will be using this script for both Windows 2008 and 2012 servers.

Remove the -Filter {Name like "%Microsoft%"} part of the script to have it return all installed software.
You probably want to be doing ForEach ($computer in $computers) rather than the other way around, assuming you're creating a $computers variable somewhere above this code with the list of computer names.
You also need to -Append to the Export-CSV command as otherwise each computers output will overwrite the output of the previous, however another issue you'll then have is knowing which software comes from which computer. You can address this by using a Calculated Property to add in the computer name to the output, as follows:
ForEach ($Computer in $Computers){
Get-WMIObject -Class win32_product -ComputerName $Computer -ErrorAction Stop |
Select-Object -Property #{N='Computer';E={$Computer}},Name,Version |
Export-Csv "C:\progrms.csv" -Append
}

Related

How to get the full list of applications like in control panel

I have been stuck at this stage of my little project.
What I try to do is list the applications that are installed and choose one of the apps to uninstall, the issue I have is that not all the apps appear, so I can't select them. For example Google chrome is not appearing while I'm using it right now to write this question.
I use this function to get all the apps:
Get-WmiObject Win32_Product -ComputerName $ComputerName | Select-Object -Property Name | Out-GridView -Title "All apps on destination Computer"
and this is whole script:
$ComputerName = Read-Host -Prompt 'Input the computer name' # the name of the computer to remove the app from
Get-WmiObject Win32_Product -ComputerName $ComputerName | Select-Object -Property Name | Out-GridView -Title "All apps on destination Computer"
$Name = Read-Host -Prompt 'Input name of the application (has to be exact name)' #name of the application
$Application = Get-WmiObject Win32_Product -ComputerName $ComputerName | Where-Object {$_.Name -eq $Name} #choose the object, this will be the app that we will delete
if ($Application) {
$Application.Uninstall()
"The removal was successful"
}
else {
$Name + ' is not installed on ' + $ComputerName
}
Start-Sleep -Seconds 10
I'm not that good with PowerShell so excuse me if this is a stupid question
Or get-package, which should be faster. Uninstall-package only works on msi providers. Powershell 5.1 only. Metadata['uninstallstring'] has the uninstall string for Programs providers. It can take wildcards and arrays as arguments. Install-package works with msi files, but without any extra options.
get-package

Powershell - how to show which computers from a list are running a specific process?

Powershell beginner here working in an air-gapped, Win7 environment with Powershell 4.0 so unable to import any modules or do anything sophisticated but wondering how I can achieve generating a txt file of computers on the network that are running a specific process, say wusa.exe for Windows Updates?
I have a txt list of all the computer names already and so far have this:
$computers = gc "C:\PCList.txt"
foreach ($computer in $computers) {Get-process | out-file -Path "C:\TheseAreRunningWusa.txt"}
But obviously that displays ALL processes, any way to cut out everything aside from a specific one but also only list the ones running said process?
Thanks in advance.
The Get-Process command allows you to specify both a remote computer to run on, and what service you are looking for.
$computers = Get-Content "C:\PCList.txt"
$output = #()
foreach ($computer in $computers) {
if(Get-Process "myProcessName" -ComputerName $computer -ErrorAction SilentlyContinue) {
$output += $computer
}
}
$output | Set-Content "C:\TheseAreRunningMyProcess.txt"
Note: I used -ErrorAction SilentlyContinue as Get-Process throws an error if the process is not found.

win32_printer not appear in wmiobject (Windows Power Shell)

I have an issue on a PC with Windows 7 Professional. I need to list the network printers in my local network, I tried to run the list object classes in PowerShell with the command:
Get-WMIObject -List | where {$_.name -match 'win32_printer'}
This shows empty, any suggestion to fix this problem?
EDIT:
My script for get the network printers is this:
Set-Location -Path C:\; get-WmiObject -class Win32_printer | ConvertTo-Json | Set-Content -Encoding utf8 C:\\xampp\\htdocs\\project\\view\\data\\printers.json
I need to list the printers in a json file, In my PC runs well, but in the PC where i need to run this script fail
Your command list out all WMI classes then filters those classes showing all of them that contain Win32_Printer. It seems that you want use:
Get-WMIObject Win32_Printer
This will list all printers that are connected to your computer (Not all of them that are on your network). Note that it will only show network printer connected to your user account.
If you are looking for all printers on your network you could list all of the queues published in Active Directory
Get-ADObject -Filter "ObjectCategory -eq 'printQueue'"
Note: This command requires the AD module from RSAT

Script to change logonworkstation in AD when running against multiple users

We are currently experiencing issues with selective user accounts switching from All Computers, to The Following Computers under Logon Workstations in Active Directory.
I'd like to have a way to have a spreadsheet (CSV) that contains usernames, and then run the PowerShell script would read these values and set each user to All Computers under Logon Workstation.
Now, here is a simple script that works perfectly fine - Only thing is that you would have to enter each username manually each time you run this command:
"Set-AdUser -Identity User.Name -LogOnWorkstations $null"
Here is what I thought would work - But ends up not being the case:
$csv = Import-Csv C:\Users\administrator\Desktop\users.csv
Set-AdUser -Identity $csv -LogOnWorkstations $null
If anyone has an idea why my second script isn't working, that would be greatly appreciated!
In the seconds script shall I assume that $csv contains a list of users? If so Set-AdUser does not accept multiple -Identity's. You would need to use a loop is the simplest answer.
$csv = Import-Csv C:\Users\administrator\Desktop\users.csv
$csv | ForEach-Object{
Set-AdUser -Identity $_ -LogOnWorkstations $null
}
This is very dependent on the structure of your CSV. If it has a column for username then you would need to update the cmdlet call.
Set-AdUser -Identity $_.UserName -LogOnWorkstations $null
If the file is just a list of users then don't even bother with Import-CSV
Get-Content C:\Users\administrator\Desktop\users.csv | ForEach-Object{
Set-AdUser -Identity $_ -LogOnWorkstations $null
}

How to pull physical path of a Windows Service using Get-Service command

I need to pull Physical Execution paths of all the Windows Services on a Set of Servers, that run on Win 2k8. As, the powershell version that is shipped with this OS is 2.0, I wanted to use Get-service command instead of Get-WmiObject.
I know that I can pull the physical path using the command given below
$QueryApp = "Select * from Win32_Service Where Name='AxInstSV'"
$Path = (Get-WmiObject -ComputerName MyServer -Query $QueryApp).PathName
I donot want this command to pull the physical path but wanted to use Get-Service command that comes with PS Version 2.0.
Any help would be much appreciated.
Even with PowerShell 3, I don't see a way to get it with Get-Service.
This 1-liner will get you the pathname, albeit with a little less of the preferred "filter left" behavior:
gwmi win32_service|?{$_.name -eq "AxInstSV"}|select pathname
Or, if you want just the string itself:
(gwmi win32_service|?{$_.name -eq "AxInstSV"}).pathname
#alroc did good, but there's no reason to filter all services. Querying WMI is like querying a DB, and you can just ask WMI to do the filtering for you:
(Get-CimInstance Win32_Service -Filter 'Name = "AxInstSV"').PathName
To explore all of the meta available for that service:
Get-CimInstance Win32_Service -Filter 'Name = "AxInstSV"' | Select-Object *
I wanted to do something similar, but based on searching / matching the path of the process running under the service, so I used the classic WMI Query syntax, then passed the results through format-table:
$pathWildSearch = "orton";
gwmi -Query "select * from win32_service where pathname like '%$pathWildSearch%' and state='Running'" | Format-Table -Property Name, State, PathName -AutoSize -Wrap
You're welcome to turn this into a one-liner by skipping defining and passing $pathWildSearch, or you could just back gwmi statement up to continue after the semi-colon.
Perhaps little less verbose,
wmic service where "name='AxInstSV'" get PathName
This should work on command prompt as well, not just powershell.
Or else if you have process name itself you could do:
wmic process where "name='AxInstSV.exe'" get ExecutablePath
To read process path you would need permission, so mostly I have better luck with service name.
I was never able to do this through the Get-Service command but if your service runs as it's own process then you can use the Get-Process command for this via the following code:
(Get-Process -Name AxInstSV).path
Source:
https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/15/powertip-use-powershell-to-find-path-for-processes/

Resources