locked
Registry Key Name/Value by powershell RRS feed

  • Question

  • Hi,

    I'm trying to check a particular registry name under system key. 

    $cn = Read-Host "Enter the host name:"

    $reg = [microsoft.win32.registrykey]::OpenremotebaseKey('localmachine',$cn)

    $regkey1=$reg.opensubkey("SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System")

    Under system Key there are n'number of  subkeys however I need check a particular subkey name. Could you please help to get it....


    Gopikrishnan.J

    Thursday, August 24, 2017 7:34 AM

Answers

  • Hi Fred,

    Your are right. But my requirement to check a particular Keyname is present or not. Below is the reg screen shot and complete script which I've created. The script is working, since I've use for loop it try to check for every subkey name which is making mess. 

    


    Gopikrishnan.J

    Your request is very vague. To find a key just look for it.

    The following tests for the existence of a key:

    $keytofind = 'SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\CAS0064E'
    if($reg.opensubkey($keytofind)){
    	Write-Host 'Key found'
    }else{
    	Write-Host 'Key not found'
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 24, 2017 8:46 AM
    • Proposed as answer by FWN Thursday, August 24, 2017 12:27 PM
    • Marked as answer by krishnan j Tuesday, August 29, 2017 8:15 AM
    Thursday, August 24, 2017 8:45 AM

All replies

  • Hi Gopikrishnan,

    if you want to retrieve a value stored in that path, that would work like this (using "Install" as example - just insert your name):

    $regkey1.GetValue("Install")

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, August 24, 2017 7:53 AM
  • Hello,

    PowerShell uses PSDrive to give you access to some resources such AD, Registry, ... with them, you can use 'TEST-PATH' and Get-ItemProperty to check key and value.

    • Check key :
    Test-Path 'HKLM:\SOFTWARE\TestSoftware'
    • Check Value :
    Get-ItemProperty -Path 'HKLM:\SOFTWARE\TestSoftware' | Select-Object -ExpandProperty 'version'


    Cdt, Loïc V. - NetSec Design - Blog: http://ms-sec.fr


    Thursday, August 24, 2017 7:59 AM
  • Hi Veirman,

    I'm trying to get data of remote machine from my local machine. Will the command works for remote machine

    Regards,

    Gopikrishnan J


    Gopikrishnan.J

    Thursday, August 24, 2017 8:23 AM
  • Hi Fred,

    Your are right. But my requirement to check a particular Keyname is present or not. Below is the reg screen shot and complete script which I've created. The script is working, since I've use for loop it try to check for every subkey name which is making mess. 

    

    $cn = Read-Host "Enter the host name:"
    $reg = [microsoft.win32.registrykey]::OpenremotebaseKey('localmachine',$cn)
    $regkey1= $reg.opensubkey("SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System")
    $state1= $regkey1.GetSubKeyNames()
    $j= $state1.Count
    $i= 0
    foreach ($state1 in $state1)
      {
        $X= $state1
        if ($x -eq 'CAS0064E')
        {
           $regkey= $reg.opensubkey("SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System\\CAS0064E")
           $state= $regkey.GetSubKeyNames()
           $regkey= $reg.opensubkey("SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System\\CAS0064E\\$state")
           $status = $regkey.Getvalue("_State")
    if ($status -eq 'Success')
        {
         $date= get-date
           write-host "GSDS task sequence status for $cn is $status, script run @ $date" -ForegroundColor green
           Write-output "GSDS task sequence status for $cn is $status, script run @ $date" | out-file .\output.log
        }
      
        else
        {
        $date= get-date
           write-host "GSDS task sequence status for $cn is $status, script run @ $date" -ForegroundColor Red
           Write-output "GSDS task sequence status for $cn is $status, script run @ $date" | out-file .\output.log
          }
           }
        Else
        {
        $date= get-date
           Write-Output "GSDS task sequence not deployed on $cn, script run @ $date" | Out-File .\output.log 
          }
      }
    


    Gopikrishnan.J

    Thursday, August 24, 2017 8:31 AM
  • That's where PSDrive is strong :

    New-Object -Type PSDrive

    https://technet.microsoft.com/en-us/library/ee176915.aspx


    Cdt, Loïc V. - NetSec Design - Blog: http://ms-sec.fr


    Thursday, August 24, 2017 8:36 AM
  • That's where PSDrive is strong :

    New-Object -Type PSDrive

    https://technet.microsoft.com/en-us/library/ee176915.aspx


    Cdt, Loïc V. - NetSec Design - Blog: http://ms-sec.fr


    Wont work with remote systems.


    \_(ツ)_/

    Thursday, August 24, 2017 8:38 AM
  • Maybe, I used it for ADDS actions on many trusted forest so it may depend on what you're trying to do. The goal is to let him learn what psdrive is.

    Else, quick look at Google and...

    https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/10/use-powershell-to-create-new-registry-keys-on-remote-systems/


    Cdt, Loïc V. - NetSec Design - Blog: http://ms-sec.fr


    Thursday, August 24, 2017 8:42 AM
  • Hi Fred,

    Your are right. But my requirement to check a particular Keyname is present or not. Below is the reg screen shot and complete script which I've created. The script is working, since I've use for loop it try to check for every subkey name which is making mess. 

    


    Gopikrishnan.J

    Your request is very vague. To find a key just look for it.

    The following tests for the existence of a key:

    $keytofind = 'SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\CAS0064E'
    if($reg.opensubkey($keytofind)){
    	Write-Host 'Key found'
    }else{
    	Write-Host 'Key not found'
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 24, 2017 8:46 AM
    • Proposed as answer by FWN Thursday, August 24, 2017 12:27 PM
    • Marked as answer by krishnan j Tuesday, August 29, 2017 8:15 AM
    Thursday, August 24, 2017 8:45 AM
  • Jrv Thank you... The above steps worked. Good learning for me...

    Gopikrishnan.J


    • Edited by krishnan j Thursday, August 24, 2017 1:27 PM typo
    Thursday, August 24, 2017 1:22 PM
  • Hi,

    Was your issue resolved? 

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, August 29, 2017 2:16 AM
  • Done...

    Gopikrishnan.J

    Tuesday, August 29, 2017 8:15 AM