Getting location information of the connected USB device - windows

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

Related

Powershell where-object not playing well with PSdrive

I'm trying to exclude two drives from a file search. I'm getting an error when running the code: "Get-ChildItem : Access to the path 'C:\Windows\system32\LogFiles...'". The search shouldn't touch C. Help!! What am I doing wrong? Code attached.
$Drives = Get-PSDrive -PSProvider FileSystem | where { -not ('c','u' -eq $_.name) }
$FS='(.*18)\.FOO'
$FPath=#(foreach($Drive in $drives) {
Get-ChildItem -Path $Drive.Root -Recurse | Where-Object {$_.Name -match $FS} -ErrorAction SilentlyContinue | %{$_.Name}
})
I think you are looking for
Get-PSDrive -PSProvider FileSystem |
Where-Object {"c","u" -notcontains $_.name} |
ForEach-Object{
Get-ChildItem -Path $_.Root -Recurse |
Where-Object {$_.Name -match '(.*18)\.FOO'} -ErrorAction SilentlyContinue |
select Name
}
Seems based on your comments you are still getting the error. Lets trouble shoot it a bit. Lets output the drive that it really seems to error on.
Get-PSDrive -PSProvider FileSystem |
Where-Object {"c","u" -notcontains $_.name} |
ForEach-Object{
$Drive = $_.Name
try{
Get-ChildItem -Path $_.Root -Recurse |
Where-Object {$_.Name -match '(.*18)\.FOO'} -ErrorAction SilentlyContinue |
Select Name
}catch{
#{
Drive = $Drive
}
}
}

Powershell command to fetch all file path for all desired files extensions

I want to search all drives using PowerShell on windows machine to get the list of all files along with their extensions -
Based on desired extension we pass in it like - *.mp3 or
Fetch all files with multiple extensions like - *.txt, *.mp3 etc.
I tried below script but its giving only information from where we are running it. But I want to scan whole machine.
Get-ChildItem -Path .\ -Filter ***.doc** -Recurse -File| Sort-Object Length -Descending | ForEach-Object { $_.BaseName }
Checkout the Get-PSDrive cmdlet. It returns a list of drives, and you can specify just disk drives with the -PSProvider FileSystem parameter:
foreach ( $drive in $(Get-PSDrive -PSProvider FileSystem) ) {
Get-ChildItem -Path $drive.Root -Filter ***.doc** -Recurse -File |
Sort-Object Length -Descending |
ForEach-Object { $_.BaseName }
}
Didn't test that but you get the idea.
Using -Include on Get-ChildItem will allow you to specify a list of extensions. The -ErrorAction will cause it to skip drives that are not available such as an unmounted CD drive.
Get-PSDrive -PSProvider FileSystem |
ForEach-Object {
Get-ChildItem -Path $_.Root -Recurse -Include '*.doc*', '*.txt' -ErrorAction SilentlyContinue |
ForEach-Object { $_.Name }
} |
ForEach-Object {[PSCustomObject]#{HashCode = $_.GetHashCode(); FullName = $_.FullName}}
} |
Export-Csv -Path $TempFile -NoTypeInformation -Encoding ASCII
Update:
Here is a better way. It will prevent unknown extensions from getting into the mix such as "Microsoft.NET.Sdk.Publish.Docker.targets."
$ExtensionList = #('.txt', '.doc', '.docx', '.mp3')
$TempFile = Join-Path -path $Env:TEMP -ChildPath "$($pid.ToString()).tmp"
Get-PSDrive -PSProvider FileSystem |
ForEach-Object {
Get-ChildItem -Path $_.Root -Recurse -ErrorAction SilentlyContinue |
Where-Object { $ExtensionList -contains $_.Extension } |
ForEach-Object {
[PSCustomObject]#{
HashCode = $_.GetHashCode();
DirectoryName = $_.DirectoryName
Name = $_.Name
}
}
} |
Export-Csv -Path $TempFile -Delimiter ';' -NoTypeInformation -Encoding ASCII
Write-Host "The temp file is $TempFile"
This is more than what the original question asked, but if you are going to go through the trouble of listing all your files, I suggest getting the filehash as well so you can determine if you have duplicates. A simple file name search will not detect if the same file has been saved with a different name. Adding to what #lit (https://stackoverflow.com/users/447901/lit) has posted:
$ExtensionList = #('.txt', '.doc', '.docx', '.mp3')
Get-PSDrive -PSProvider FileSystem |
ForEach-Object {
Get-ChildItem -Path $_.Root -Recurse -ErrorAction SilentlyContinue |
Where-Object { $ExtensionList -eq $_.Extension } |
## ForEach-Object { $_.Name, $_.FullName, $_.GetHashCode() }
Select-Object #{Name="Name";Expression={$_.Name}}, #{Name="Hash";Expression={$_.GetHashCode()}}, #{Name="FullName";Expression={$_.FullName}} |
Export-Csv -Path C:\Temp\testing.csv -NoTypeInformation -Append
}
The addition of the file hash will allow you to see if you have duplicates and the full name will allow you to see where they are located.

Search for registry key path

please, could you help me with searching for registry path?
I am trying to find path of REG_BINARY with name 00036601 in HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook\20424cec73cea54ab3d011f91bf036b2
I have problem because last folder(20424cec73cea54ab3d011f91bf036b2) in path is different on every laptop. Cannot find any working solution with REG QUERY in cmd or powershell.
I know how to find it in known path or list all subkeys, but failed to filter one value.
So i want to get output like: key name 00036601 found in HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook\20424cec73cea54ab3d011f91bf036b2
EDIT: sorry for my english, maybe i am notz describing it correctly, please, could you look on image?
Regedit
I am looking for string name 00036601 - marked in image. Thanks for help
EDIT2: i found way how to do it with cmd "REG QUERY HKCU\SOFTWARE\Microsoft /s /f 00036601"
But not with powershell...
You can search the registry with PowerShell. I do not have the same registry path as you have.
Get-ChildItem -Path HKCU:\Software\Microsoft\Office\16.0 `
-Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.PSChildName -eq '00036601' }
If you must do it from a cmd.exe shell:
powershell -NoLogo -NoProfile ^
"Get-ChildItem -Path HKCU: -Recurse -ErrorAction SilentlyContinue | " ^
"Where-Object { $_.PSChildName -eq '00036601' }"
EDIT: that was never going to get there.
This works on my machine to find the IM enabled setting.
$r = Get-ChildItem -Path 'HKCU:/Software/Microsoft/Office/' -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Property -eq 'EnablePresence' }
(Get-ItemProperty -Path $r.PSPath).EnablePresence
Please try this on your machine.
$r = Get-ChildItem -Path 'HKCU:/Software/Microsoft/Office/' -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Property -eq '00036601' }
(Get-ItemProperty -Path $r.PSPath).'00036601'

need to search files in windows with latest time stamps through powershell

i have project assigned to me, first i need to find file in which directory it is in server, code i have written
$drive = get-psdrive |select root |select-string -pattern ':'
Write-Host $drive
foreach ($a in $drive)
{
Get-ChildItem $a -recurse -filter "*DBaEnvProd*" |select directory
}
there shd be one output a there will be only one dbenvprod on server
how to get one value
output iam getting
Get-ChildItem : Cannot find drive. A drive with name '#{Root=C' does not exist.
At D:\temp.ps1:6 char:26
+ Get-ChildItem <<<< $a -recurse -filter "*DBaEnvProd*" |select directory
Get-ChildItem : Cannot find drive. A drive with name '#{Root=D' does not exist.
At D:\temp.ps1:6 char:26
+ Get-ChildItem <<<< $a -recurse -filter "*DBaEnvProd*" |select directory
Get-ChildItem : Cannot find drive. A drive with name '#{Root=E' does not exist.
At D:\temp.ps1:6 char:26
+ Get-ChildItem <<<< $a -recurse -filter "*DBaEnvProd*" |select directory
Get-ChildItem : Cannot find drive. A drive with name '#{Root=F' does not exist.
At D:\temp.ps1:6 char:26
+ Get-ChildItem <<<< $a -recurse -filter "*DBaEnvProd*" |select directory
Get-ChildItem : Cannot find drive. A drive with name '#{Root=Z' does not exist.
At D:\temp.ps1:6 char:26
+ Get-ChildItem <<<< $a -recurse -filter "*DBaEnvProd*" |select directory
Add -InputObject parameter:
Select-String -InputObject {$_.Root} -Pattern ':'
Get-PSDrive | ForEach-Object {Get-ChildItem -LiteralPath $_.Root -Filter "*DBaEnvProd*" -Recurse}

Lazy loading in powershell?

Can we defer a variable initialization untill it is needed ?
What I would like to do is predefine some variables in my profile that will contain a list of AD computer:
let's say I want:
$OU1_workstation to be fill with computers found in OU=workstations,OU=OU1,dc=domain,dc=com
$OU2_workstation fill with computers found in
OU=workstations,OU=OU2,dc=domain,dc=com and so on...
I use the following script to do it but it takes 30sec to compute, so currently I can't put that in my profile...
Get-ADOrganizationalUnit -SearchScope onelevel -Filter "*" -Properties "name","distinguishedname" |%{
set-Variable -Name "$($_.name)_workstation" -value (Get-ADComputer -Searchbase "OU=workstations,$($_.Distinguishedname)" -Filter * )
}
What options are available in powershell ?
Finally, based on #Richard's reply of a previous question of mine, I've chosen the following path to achieve some sort of lazy loading : using a scriptproperty of a PSCustomObject.
So I can put this in my profile
#requires -module activedirectory
$server=New-Object PSCustomObject
Get-ADOrganizationalUnit -SearchScope onelevel -Filter "*" -Properties "name","distinguishedname" |
?{
$_.name -notmatch 'Administrateurs|Administration|Comptes de deploiement|Contacts|Domain Controllers|Groupes|Serveurs|Services'
} |
%{
$OU=$_.name
$s=[scriptblock]::Create("Get-ADComputer -SearchBase ""OU=servers,OU=$OU,DC=domain,DC=com"" -Filter 'name -notlike "" *old""' |select -expand name")
$server| Add-Member -MemberType ScriptProperty -name $OU -value $s -Force
}
then when needed I can call $server.OU1 to get all server under this OU, $server.OU2 etc...

Resources