i am using this powershell command to fetch a particular user profile
"Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"
But when i am using this same command in cmd by invoking powershell i am getting
'Where-Object is not recognized as an internal or external command,operable program or batch file'
The command i am running in cmd is as follows :-
"powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"
I need to run this command from cmd only , i don't have any other options .
So please give me an alternative to "Where-Object"
So please give me an alternative to "Where-Object"
powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela
You don't need an alternative. The above command is failing because the pipe | is being interpreted by the cmd shell and not by PowerShell.
If you escape the pipe ^| then the piping is done by the PowerShell command as expected:
powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\Pela
Example:
F:\test>powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\DavidPostill'}
__GENUS : 2
__CLASS : Win32_UserProfile
__SUPERCLASS :
__DYNASTY : Win32_UserProfile
__RELPATH : Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
__PROPERTY_COUNT : 12
__DERIVATION : {}
__SERVER : HAL
__NAMESPACE : root\cimv2
__PATH : \\HAL\root\cimv2:Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
LastDownloadTime :
LastUploadTime :
LastUseTime : 20160822200129.697000+000
Loaded : True
LocalPath : C:\Users\DavidPostill
RefCount : 146
RoamingConfigured : False
RoamingPath :
RoamingPreference :
SID : S-1-5-21-1699878757-1063190524-3119395976-1000
Special : False
Status : 0
PSComputerName : HAL
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
syntax - Escape Characters, Delimiters and Quotes.
Related
Something similar to echo $$; python my_script.py > out.log
Expected output:
using cmd_command or powershell command
If you are running PowerShell, the PID variable is available.
PS C:\> $PID
8420
PS C:\> Get-CimInstance -Class CIM_Process | Where-Object { $_.ProcessId -eq $PID }
ProcessId Name HandleCount WorkingSetSize VirtualSize
--------- ---- ----------- -------------- -----------
8420 pwsh.exe 1004 119939072 2204255981568
See the other members available using:
Get-CimInstance -Class CIM_Process | Where-Object { $_.ProcessId -eq $PID } | Format-List -Property * -Force
or
Get-CimInstance -Class CIM_Process | Get-Member
I am automating an application deployment.
After installing and starting the service via Choco, I am trying to change the Service StartMode to Auto, but I am getting 21 as the returnvalue and hence the mode is not changed.
PS C:\Windows\system32> $appservice = Get-WmiObject -Class Win32_Service -Filter "Name='<app_name>'"
PS C:\Windows\system32> $appservice.ChangeStartMode("Auto")
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 21
PSComputerName :
PS C:\Windows\system32> Get-WmiObject -Class Win32_Service -Filter "Name='<app_name>'"
ExitCode : 0
Name : <app_name>
ProcessId : 3180
StartMode : Manual
State : Running
Status : OK
Kindly help.
TIA
Try
Get-Service -Name 'TheServiceName' | Set-Service –StartupType Automatic
I have been working this for a couple days now, and no matter how I run this and work it, it seems to uninstall the program via PowerShell and returns the success code:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
This happens with various notoriously difficult to remove software such as McAfee.
The command being used is:
Get-WmiObject -Class win32_product -Filter "Name like '%McAfee%'" | ForEach-Object {$_.Uninstall()}
I've tried various scripts, solutions here, and variations of these (such as below).
$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString
$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString
if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall64 = $uninstall64.Trim()
Write "Uninstalling (x64)..."
start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait
}
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
Write "Uninstalling (x32)..."
start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait}
Even something simple like Yahoo Messenger, the command fails to uninstall the application when run from a Powershell Window as Administrator yet returns a success code and/or is no longer present on the WMI application list.
You can check the MSIInstaller events to find a clue why the uninstall failed:
Get-WinEvent -computername <computername> -ProviderName MSIInstaller -Maxevents 30
You can also log the MSI activity with /le '<logfilepath>' added to your invocation of msiexec.exe and check the results.
I believe the msi install/uninstall operations are asynchronous. You may have to wait within your pssession until the install is finished.
McAfee Agent sometimes requires frminst.exe /forceuninsall to be removed.
I am trying to use the following code to make a beep on a remote computer through Powershell:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "[console]::beep(500,300)" -ComputerName "mycompname"
In addition I have used [System.Media.SystemSounds]::Beep.Play() in place of the console command.
It doesn't give any error codes and outputs this:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId :
ReturnValue : 9
PSComputerName :
I am running this command with elevated Powershell and I am an admin on the network. Using Invoke-Command DOES NOT work on my computer, so I am opting for Invoke-WmiMethod instead. The following code DOES actually work, so I don't understand why the beep one won't:
Invoke-WmiMethod -path Win32_Process -Name Create -ArgumentList "msg * 'hello'" -ComputerName "mycompname"
Final notes: I would like to be able to use Invoke-WmiMethod to do remote shutdown and taskkill, but those functions also do not work, only sending a message works. Any help would be greatly appreciated.
May be can you try this (modify username and password)
$Username = 'labuser'
$Password = 'labuser'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-command –computername "mycompname" -credential $Cred –scriptblock {[console]::beep(500,300)}
TessellatingHeckler was right. I just needed to change the code so that the receiving computer knows that I am using powershell:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "powershell.exe [console]::beep(500,300)" -ComputerName "mycompname"
This also works for everything else:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "powershell.exe shutdown -s" -ComputerName "mycompname"
Thanks!
what is the powershell command to read the following registry entry?
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_03A8&PID_0258\6&af75239&0&3\LocationInformation
I tried the following code, i used to get the Device information only
gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} |
Sort Manufacturer,Description,DeviceID |
Ft -GroupBy Manufacturer DeviceID
how to get the location information of the connected usb device?
Look at Get-ItemProperty Cmdlet with registry Provider Path.
For example,
Get-ItemProperty -path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
Try this for getting location information for all USB devices:
$devid = gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Select -ExpandProperty DeviceID
$devid | % { Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Enum\$_" -Name LocationInformation -ErrorAction SilentlyContinue}
Are you looking for that :
Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Enum\USB\VID_03F0&PID_1F1D\5&3aded796&0&2' -Name LocationInformation
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_03F0&PID_1F1D\5&3aded796&0&2
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_03F0&PID_1F1D
PSChildName : 5&3aded796&0&2
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
LocationInformation : Port_#0002.Hub_#0004
Or
(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Enum\USB\VID_03F0&PID_1F1D\5&3aded796&0&2' -Name LocationInformation).LocationInformation
Port_#0002.Hub_#0004
You can get the connected devices using :
Get-WmiObject Win32_USBHub
You just need to join the two results for exemple for my hard drive :
$PnpdeviceId = (gwmi win32_USBHub | where { $_.name -like '*stockage*'}).PNPDeviceID
(Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Enum\$PnpdeviceId" -Name LocationInformation).LocationInformation