I want to list the files of C: drive. First of all, I want to get the device ID from logical disk wmi object, and list it.
Below command returns:
Get-WmiObject -class Win32_logicaldisk
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 940371968
Size : 125809192960
VolumeName :
But this command:
Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path {$_}
gives below error:
Get-ChildItem : Cannot find drive. A drive with the name
'#{deviceid=C' does not exist. At line:1 char:60
+ Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path { ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{deviceid=C:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem -path accepts pipeline input, how we can solve this ?
Your Select is returning an Object with a property named DeviceID.
Use -ExpandProperty to get the property value, then pipe that:
Get-WmiObject -class Win32_logicaldisk | select -expandproperty deviceid | Get-ChildItem -path {$_}
You could also just select the property in the Object that gets returned. In this case, $_.DeviceID
Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path {$_.DeviceID}
Related
I have a powershell script below which should logically work but throws below error saying "A drive with the name 'E" does not exist." But in fact it exists. This error comes when I input the drive using a variable, but if I input the drive that is path manually like "E:" it will work ok. Do no know what I am doing wrong.
Get-ChildItem : Cannot find drive. A drive with the name 'E' does not exist.
At line:24 char:10
+ $list = Get-ChildItem -path $CDDriveLetterToText
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (E:String) [Get-ChildItem], DriveNotFoundExcepti
on
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
My code is as below.
Function Image-Windows10 () {
$CDDrives = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -Property name
$FlashDrives = get-wmiobject win32_diskdrive | where {$_.InterfaceType -eq 'SCSI'} | select-object -property index, size
[int]$NumberOfFlashDrives=$FlashDrives.Count
[int]$NumberOfCDDrives=$CDDrives.Count
$CDDriveLetterToText = Out-String -inputObject $CDDrives.Get($NumberOfCDDrives-1)
$CDDriveLetterToText = $CDDriveLetterToText.Replace("name","").Replace("----","").Replace("`n","").Replace(" ","")
$list = Get-ChildItem -Path $CDDriveLetterToText
}
Image-Windows10
instead of selecting the name you might want to go for the property DriveLetter. the parameter -ExpandProperty will return an array of the specified value - thus no need to manipulate the string'
# get all drive letters from devices of type 'CDRom'
$driveArray = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -ExpandProperty DriveLetter
# this array can then be iterated like
foreach($drive in $driveArray) {
$list = Get-ChildItem -Path $drive
}
I am trying to filter all methods named 'Create' in all workspaces as part of a learning Powershell exercise.
I have this command which seems to list all the methods but I am unable to filter on 'Create' when I pipe the output of teh below to either -Filter or where.
What am I doing wrong here?
Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods
With -Filter
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
-Filter : The term '-Filter' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:86
+ ... Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (-Filter:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Or select where
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | select name
-eq "Create"
Select-Object : A parameter cannot be found that matches parameter name 'eq'.
At line:1 char:98
+ ... _.methods} | select -ExpandProperty Methods | select name -eq "Create ...
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
If you want to list the actual Create methods themselves:
Get-WmiObject -List |
Select-Object -ExpandProperty Methods |
Where-Object Name -eq Create
If, by contrast, you want to list the classes that have a Create method:
Get-WmiObject -List | Where-Object { $_.Methods.Name -contains 'Create' }
As for what you tried:
-Filter is a parameter, yet you tried to use it as a command. It's also unclear what you're trying to filter, given that there's no argument.
select name -eq "Create" mistakenly applies the Where-Object syntax to the Select-Object (select) cmdlet.
In both attempts:
the use of Where-Object {$_.methods} is unnecessary, because the
Select-Object -ExpandProperty Methods call will automatically ignore method-less classes.
-List by itself is enough to list all classes; no need for wildcard *.
I am trying to copy files to any local drive with volume named "Data", but I am unsure how to parse out the drive letter for the copy.
This is the line I am using to grab the volumes:
$drive=Get-WmiObject -class Win32_logicaldisk |
Where-Object {$_.VolumeName -eq "Data"} |
select DeviceID
Then I want to do an xcopy from c:\temp to $drive/backupfolder (this is where it fails as $drive shows
DeviceId
--------
D:
$drive isn't a string. But an Object with the property DeviceId which is a string. You can either expand the property
$drive = Get-WmiObject -Class Win32_logicaldisk |
Where-Object {$_.VolumeName -eq "Data"} |
Select-Object -ExpandProperty DeviceID
Or expand the property this way:
$drive = (Get-WmiObject -Class Win32_logicaldisk | Where-Object {$_.VolumeName -eq "Data"}).DeviceID
Or reference the property on the variable:
"$($drive.DeviceID)\backupfolder"
I have a Powershell script that lists all the users/groups in the local administrators group for all computers in a designated OU in Active Directory.
The script works perfectly locally (if I run it against the local machine only) but when I run it against remote machines, it technically works but it throws a consistent error that I don't know how to filter out.
Here is the script (NOTE: must be running from ActiveDirectory PS console to use Get-ADComputer):
Get-ADComputer -SearchBase 'OU=ou01,dc=domain,dc=local' -Filter 'ObjectClass -eq "Computer"' `
| ForEach-Object {
Get-WmiObject win32_groupuser -cn $_.name -ErrorAction SilentlyContinue `
| Where-Object { $_.groupcomponent -match 'administrators' } `
| ForEach-Object -ErrorAction SilentlyContinue {[wmi]$_.partcomponent } `
| Select-Object __SERVER,Caption
} | Format-Table -Property * -AutoSize
Here are the results (correct result is in first line, error below that):
__SERVER Caption
-------- -------
workstation_name workstation_name\Administrator
Cannot convert value "\\workstation_name\root\cimv2:Win32_Group.Domain="DOMAIN",Name="Domain Admins"" to type "System.Management.ManagementObject". Error: "Not found "
At line:1 char:306
+ Get-ADComputer -SearchBase 'OU=ou01,dc=domain,dc=local' -Filter 'ObjectClass -eq "Computer"' | ForEach-Object { Get-WmiObject win32_groupuser -cn $_.name -ErrorAction SilentlyContinue | Where-Object { $_.groupcomponent -match 'administrators' } | ForEach-Object -ErrorAction SilentlyContinue {[wmi]$_. <<<< partcomponent } | Select-Object __SERVER,Caption } | Format-Table -Property * -AutoSize
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
I have unsuccessfully tried to use -ErrorAction SilentlyContinue, is there another way to suppress this message? Not sure what I am missing.
I am trying to get the OWNER of a process, code :
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string**
This works great under win8 but in win7 I get this msg :
ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user" val
ue of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Program Files (x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1 char:
108
+ (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'
}).getowner() | Foreach-Object <<<< user | out-string
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
ell.Commands.ForEachObjectCommand
Help please! Thank you for the time.
Instead of foreach-object user, use select -expand user. This is equivalent to doing foreach-object { $_.user } which is probably what you meant to do. Improvements to flexibility in the grammar allow your first attempt in later versions of powershell.
The older version of Powershell won't work with the simplified syntax. This should work on either one:
(Get-WmiObject -class win32_process |
where{$_.ProcessName -eq 'explorer.exe'}).getowner() |
Foreach-Object { $_.user | out-string }
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | select user
I had a similar problem but in my case, there was a non-printable character in my script that appeared after one of the }'s. ASCII code 03. I found that by opening the script in a binary editor (Textpad8). I deleted this character and it fixed the problem for me.