How do I (un)install an application remotely using PowerShell 2.0? - powershell

I understand how to use PowerShell to install/uninstall locally, but how can I run this on another server that is on the same network and same OS version?
$product = Get-WmiObject -Class Win32_Product -Filter "Name='MyMSI'"
$product.Uninstall()
or
$product = Get-WmiObject -List | ?{ $_.Name -eq "Win32_Product" }
$product.Install("C:\\MyMSI.msi")

You can still use those lines to uninstall and install software by using the -ComputerName property and specifying the name of the computer. For the install you have to copy the software to the local filesystem and specify that in the command.

You must use PowerShell remoting.
Read this please

Related

How to find the version of Trend from the Registry of a specific PC?

I was wondering if there is a way to find a registry value of a specific computer. The only way I could find is entering a pssession and then exiting.
$Computer = Read-Host "Enter the PC Name: "
$connection=test-connection -ComputerName $Computer -Quiet
if($connection -eq $True) {
Enter-PSSession $Computer
$TrendServer= Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TrendMicro\PC-cillinNTCorp \CurrentVersion | Select Server
write-output $TrendServer
if($TrendServer -ne $null){
Exit-PSSession
}
} else{Write-Output "Computer is not available. Please check Lan Sweeper "}
If it is installed using Windows Installer, you can use WMI, though this class is known to be quite slow:
Get-CimInstance -Query "SELECT * FROM Win32_Product WHERE Name = 'TrendMicro'" `
-ComputerName $computer
Change the name from 'TrendMicro' to whatever it actually is (I don't have it installed to check), and for older versions of PowerShell, use Get-WmiObject instead of Get-CimInstance.
Get more information here: Working with Software Installations

Finding path of process on remote machine

You can use the following in Powershell to obtain the full path to where a specific process is running:
Get-Process | where{$_.Name -like "*iexplore*"} | Select Path
If I want to find this path for a service on a remote machine, I thought I could just utilise the following:
Get-Process -ComputerName $MyServer | where{$_.Name -like "*iexplore*"} | Select Path
However, this doesn't return anything. I can see that I can find the service itself with some details on current usage etc. but I cannot find the path for where the .exe file is located. (I also noticed I cannot see how many CPUs the process is using either).
Is there a way to find the path for the process?
Get-Process missing this, but you can use WMI:
Get-WmiObject -Class win32_process -ComputerName $MyServer -Filter 'name like "%iexplore%"' | select path

How to know the installed Software version in PowerShell

I wanted to know what version of software(say X software) installed in my system with PowerShell. In my case, I wanted to know what version of Service Fabric SDK installed in my system with PowerShell.
if your process/software is run , use this command :
Get-Process -Name "xsoftware" | Format-list -Property ProductVersion
In our case, we needed to verify if MongoDB was installed on multiple servers, and if so, what version. We used a simple PowerShell command and pushed it out multiple servers via Ansible/AWX. Here's the command:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -like "*MongoDB*"} | Select-Object DisplayName, DisplayVersion, InstallDate | Format-List;
This link highlight very well what the issues are with using the WMI approach and then outlines a solution for using the registry. The way he does the registry also allows for calling it against remote machines.
https://mcpmag.com/articles/2017/07/27/gathering-installed-software-using-powershell.aspx
You can use WMI to get information of an installed product. To do this filter by the Name. For example, to get information of Word:
$product = gwmi win32_product -filter "Name LIKE '%Word%'"
You will then find the version information in the Version property:
$product.Version
NOTE: The WMI lookups can be a bit slow, so be patient!

Discover and install network printers via powershell

I am new to PowerShell and I am trying to write a script that will install a series of network printers for me. To get me started I was looking for a way to find all shared printers on a print server and then install them locally. Here is something that doesn't work but gets the idea across. One thing to note is that this is this script is being run on a win 2008 server.
Get-WmiObject -computername $printServer -class Win32_Printer | Where {$_.name -notlike "Microsoft*"} | add-printer -connectionname \\$_.systemName\$_.shareName
I don't currently have a way to test this but I believe that this may work for you.
$printClass = [wmiclass]"win32_printer"
Get-WmiObject -computername $printServer -class Win32_Printer | ? {$_.name -notlike "Microsoft*"} | % { $printClass.AddPrinterConnection([string]::Concat("\\", $_.systemName, "\", $_.shareName)) }

Open command prompt to access folders of a USB connected windows phone

I am trying to open a command prompt to access folders of a USB connected windows phone. I have tried several commands like the following but to no avail.
wmic logicaldisk get name
GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType -eq 'USB' }
Could someone suggest me the best way to accomplish this without using any tool?
My task is to access the mobile device to adjust language settings using PowerShell commands.
Phone : Lumia 1020 running Windows Phone 8.
To get a list of USB drives attached to the PC, execute this command.
Get-WmiObject Win32_Volume -Filter "DriveType='2'"
If your mobile is attached as a USB disk, it should show up. From the data you get back, you should be able to extract things like Caption, Label, Name and DriveLetter. Then you can automate things a little bit further:
cd (Get-WmiObject Win32_Volume -Filter "DriveType='2'" | Where-Object label -eq "YourDiskName").DriveLetter
EDIT: Since Get-WmiObject command is now depreciated, the preferred way is now to use Get-CimInstance.
Get-CimInstance -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=2"
You can get the phone's top-level directory using this function, then add the root folder name from explorer e.g. 'Internal Shared Storage', 'Card', etc.
function Get-PhoneMainDir($phoneName) {
$o = New-Object -com Shell.Application
$rootComputerDirectory = $o.NameSpace(0x11)
$phoneDirectory = $rootComputerDirectory.Items() | Where-Object {
$_.Name -eq $phoneName } | select -First 1
if ($phoneDirectory -eq $null) {
throw "Not found '$phoneName' folder in This computer. Connect your phone."
}
return $phoneDirectory;
}
$phoneName is the name of the phone in Explorer, e.g. 'Pixel 5a'
A full example is available here:
https://github.com/nosalan/powershell-mtp-file-transfer/blob/master/phone_backup_recursive.ps1