locked
Unable to find and delete registry keys using powershell -Error - Remove-Item : A device attached to the system is not functioning. RRS feed

  • Question

  • Hi,

    I am not able to make this script run. Please help me.

    $systems = get-content "C:\cb\systems.txt"
    Write-Host $systems
    foreach ($system in $systems)
    {

    Invoke-Command -ComputerName $system{ Pushd

    sl HKLM:SOFTWARE\

    $path = Test-Path cbd
    write-host $path

    Remove-Item cbd

    popd
    }}

    Wednesday, November 27, 2019 4:49 PM

Answers

  • This is how this has to be done.

    $sb = { 
        if(Test-Path HKLM:SOFTWARE\cbd){
            write-host 'Path found'
            Remove-Item cbd
        }else{
            write-host 'Path NOT found'
        }
    }
    Invoke-Command -ComputerName $system -ScriptBlock $sb 


    \_(ツ)_/

    • Marked as answer by Calvin.Hobbesr Wednesday, November 27, 2019 5:35 PM
    Wednesday, November 27, 2019 5:30 PM
  • Please format your code as code: How to Use the Code Feature in a TechNet Forum Post.

    Please do not use aliasses in scripts. You should read:  The Unofficial PowerShell Best Practices and Style Guide.

    That's all you need actually:

    $ComputernameList = get-content "C:\cb\systems.txt"
    foreach ($Computername in $ComputernameList) {
        Invoke-Command -ComputerName $Computername { 
            Remove-Item 'REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\cbd'
        }
    }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by Calvin.Hobbesr Wednesday, November 27, 2019 5:35 PM
    Wednesday, November 27, 2019 5:31 PM

All replies

  • This is how this has to be done.

    $sb = { 
        if(Test-Path HKLM:SOFTWARE\cbd){
            write-host 'Path found'
            Remove-Item cbd
        }else{
            write-host 'Path NOT found'
        }
    }
    Invoke-Command -ComputerName $system -ScriptBlock $sb 


    \_(ツ)_/

    • Marked as answer by Calvin.Hobbesr Wednesday, November 27, 2019 5:35 PM
    Wednesday, November 27, 2019 5:30 PM
  • Please format your code as code: How to Use the Code Feature in a TechNet Forum Post.

    Please do not use aliasses in scripts. You should read:  The Unofficial PowerShell Best Practices and Style Guide.

    That's all you need actually:

    $ComputernameList = get-content "C:\cb\systems.txt"
    foreach ($Computername in $ComputernameList) {
        Invoke-Command -ComputerName $Computername { 
            Remove-Item 'REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\cbd'
        }
    }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by Calvin.Hobbesr Wednesday, November 27, 2019 5:35 PM
    Wednesday, November 27, 2019 5:31 PM