Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Open-Registry{
- [CmdletBinding()]
- [Alias("regJump")]
- param(
- [Parameter(Position=0)]
- $regKey
- )
- #check for clipbaord only if no argument provided
- if (!$regKey){
- #split the clipboard content by crlf and get of trailing crlf in case clipboard populated via piping it to clip.exe
- $cmd = {
- Add-Type -Assembly PresentationCore
- [Windows.Clipboard]::GetText() -split "`r`n" | where {$_}
- }
- #in case its run from the powershell commandline
- if([Threading.Thread]::CurrentThread.GetApartmentState() -eq 'MTA') {
- $regKey = & powershell -STA -Command $cmd
- }
- else {
- $regKey = & $cmd
- }
- }
- foreach ($key in $regKey){
- $replacers = @{
- 'HKCU:?\\'='HKEY_CURRENT_USER\'
- 'HKLM:?\\'='HKEY_LOCAL_MACHINE\'
- 'HKU:?\\'='HKEY_USERS\'
- 'HKCC:?\\'='HKEY_CURRENT_CONFIG\'
- 'HKCR:?\\'='HKEY_CLASSES_ROOT\'
- }
- #replace hive shortnames with or without PowerShell Syntax + remove trailing backslash
- $properKey = $key
- $replacers.GetEnumerator() | foreach {
- $properKey = $properKey.ToUpper() -replace $_.Key, $_.Value -replace '\\$'
- }
- #check if the path points to an existing key or its parent is an existing value
- #add one level since we don't want the first iteration of the loop to remove a level
- $path = "$properKey\dummyFolder"
- #test the registry path and revert to parent path until valid path is found otherwise return $false
- while(Split-Path $path -OutVariable path){
- $providerPath = $providerPath = "Registry::$path"
- if (Test-Path $providerPath){
- break
- }
- }
- if ($path){
- Set-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\ -Name LastKey -Value $path -Force
- #start regedit using m switch to allow for multiple instances
- $regeditInstance = [Diagnostics.Process]::Start("regedit","-m")
- #wait the regedit window to appear
- while ($regeditInstance.MainWindowHandle -eq 0){
- sleep -Milliseconds 100
- }
- }
- else{
- Write-Warning "Neither ""$key"" nor any of its parents does exist"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement