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
Related
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
I've tried a variety of iterations of this and gotten a range of errors. I'm trying to get a a list of installed drivers off from a list of computers. None of the ways I've tried in PowerShell have piped the information into a csv. Here's the current iteration of the script.
#Load Active Directory
Import-Module activedirectory
#Load list of computers
$results = #()
$Computer = Get-Content -path 'C:\ScriptResources\computers.txt'
#Check each computer in the list
foreach($ComputerName in $Computer)
{
$results += Get-ADComputer -Filter " Name -Like '*$ComputerName*' " | Get-PrinterDriver; Start-Sleep -milliseconds 500
}
#Export to CSV file
$results | export-csv 'C:\ScriptResults\InstalledPrinters.csv'
I've also used it with just the Get-Printer command and got the following error.
Get-Printer : No MSFT_Printer objects found with property 'Name' equal to 'Redacted'. Verify the value of the
property and retry.
Depending what I've fed the $Computer file I'll get different errors. I've also gotten the RPC server is unavailable and Error Spooler Service Not Running. I have domain wide privileges and I checked the print spooler service and it is running.
The reason I think this is odd is that I have .bat tool that I use that gets printer info from a singular host and I don't run into any issues. The reason I'm trying to put this in PowerShell is because 1) I want to do the whole domain and 2) PowerShell formats its outputs in a more useable fashion.
wmic /node:%ComputerIP% path win32_printer get deviceid, drivername, portname
Additionally, I've also tried the following in the $results function of the script
$results += Get-WmiObject -class Win32_printer -ComputerName name, systemName, shareName
This didn't give errors. What it did instead is that for each computer in the list of computers it checked the computer I was running the script from for its printers and output on each line which printers were installed on my computer.
I'm at a loss and any help would be appreciated. Thanks!
Just so this is closed out. Vivek's answer ended up working.
$results += Get-WmiObject -class Win32_printer -ComputerName $Computer | Select name, systemName, shareName
The RPC issue I was getting was that the list of computers were all turned off for some reason (remote site + different time zone + doing the testing during second shift). Normally, everything remains on though. So that was just an anomaly.
Thanks for the help!
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)) }
I'm trying out a script to go grab installed software on servers remotely. Problem is I want it to output certain attribs including the computer name but I can't seem to figure out how to get the name inserted.
Here is what I have so far...
$servers = Get-QADComputer -SearchRoot "OU=servers,OU=mydomain:-),DC=COM" | Select Name
...which works fine of course. Then...
$servers | % {Get-WMIObject -Class Win32Reg_AddREmovePrograms} | select Displayname,Version,InstallDate,PSComputerName
... which provides the full list of software installed on all servers in that OU but the PSComputerName becomes MY COMPUTER (the computer I run the query from - not the computername of the system being queried). The goal is to have the servername the software is installed on on each line item of software. I've asked professor Google and don't seem to see anything helpful (or anything that I understand anyway).
Hope this makes sense. semi-amateur PS script writer so hopefully this is easy for you guys. Thanks in advance for your help
Your command:
Get-WMIObject -Class Win32Reg_AddREmovePrograms
Does not specify computer to query, so it just query computer command being executed on. Thus PSComputerName display MY COMPUTER, as MY COMPUTER is computer being queried. You have to specify -ComputerName parameter to Get-WMIObject cmdlet to query specific computer. And -ComputerName parameter accept array of computer names, so you can put array of computer names to it instead of using ForEach-Object cmdlet and query one computer at time.
Since the object returned from the WMI call doesn't contain the computer you made the request on, you need to include it yourself from include your ForEach-Object (%) block. You could use Add-Member to add it yourself, then do your Select-Object outside like you're doing now:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -ComputerName $_ |
Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
} | select Displayname,Version,InstallDate,ComputerName
Another way is to move the Select-Object to inside the block and do it within there, by creating a new property on the fly with a hashtable:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -computername $_ |
Select-Object Displayname,Version,InstallDate,#{Name='ComputerName';Expression={$_}}
}
So I'm trying to get all the locally installed applications and put them into a listbox. However, I'm having some problems. Whenever I use the below code:
$prog = (get-wmiobject win32_product -computer $current_hostname.text -property Name).Name
foreach($program in $prog)
{
$program_list_current.items.add($program)
}
Whats returned in the listbox is the applications plus some other text/string at the beginning of each app. In some cases, where '-property Name' is replaced with ' | select Name', nothing is returned at all.
I'm using the above syntax because the below code works (which gets the AD groups for a machine and puts each group into a listbox):
$processnames_t = (Get-ADComputer -Identity $current_hostname.text -Property MemberOf).MemberOf
foreach ($processname in $processnames_t)
{
[void]$AD_list_current.Items.Add($processname)
}
Any ideas as to why it works for the AD groups but not the installed apps? Maybe something to do with the nature of get-wmiobject?
Thanks
I couldn't really repro the issue you are seeing. But, for the program names, you can replace
$prog = (get-wmiobject win32_product -computer $current_hostname.text -property Name).Name
with
$prog = get-wmiobject win32_product -computer $current_hostname.text | Select -Exp Name
Select Name alone won't work as it returns the object and not a string. In this case, to use it as a listbox item, you need the string.