locked
Verify if Path exists based on gwmi query result RRS feed

  • Question

  • Hello 

    I would like to know if there is a better way to do this on Powershell 2.0 

    get-wmiobject win32_userprofile | where {$_.LocalPath -match '^C:\\users\\user[ds]a'} | select Localpath | ft -HideTableHeaders  | out-file output.txt
    gc output.txt | % {test-path $_ }
    

    First I ran a query to look for any user folder that begins with path c:\users\Userda* or C:\users\usersa* then select the localpath but I am not able to directly test-path the result, I understand it is because the property is not a name from gci command. 
    I need to query based on the gwmi results because I want to delete profiles. So I would do something like gwmi get me the user profiles I want, then delete those, then test-path if those selected profiles actually exist or not. 


    • Edited by nakamed Saturday, August 5, 2017 8:46 PM
    Saturday, August 5, 2017 8:45 PM

Answers

  • For any version of PowerShell this is how to do this:

    get-wmiobject win32_userprofile | 
    	Where-Object { $_.LocalPath -match '^C:\\users\\user[ds]a' } | 
    	ForEach-Object{ test-path $_.LocalPath }

    To delete a profile use "$_.Delete()" - no need to know the path.  Deleting the path will not remove a profile from the registry.


    \_(ツ)_/


    • Marked as answer by nakamed Saturday, August 5, 2017 10:06 PM
    • Edited by jrv Saturday, August 5, 2017 10:44 PM
    Saturday, August 5, 2017 8:59 PM

All replies

  • For any version of PowerShell this is how to do this:

    get-wmiobject win32_userprofile | 
    	Where-Object { $_.LocalPath -match '^C:\\users\\user[ds]a' } | 
    	ForEach-Object{ test-path $_.LocalPath }

    To delete a profile use "$_.Delete()" - no need to know the path.  Deleting the path will not remove a profile from the registry.


    \_(ツ)_/


    • Marked as answer by nakamed Saturday, August 5, 2017 10:06 PM
    • Edited by jrv Saturday, August 5, 2017 10:44 PM
    Saturday, August 5, 2017 8:59 PM
  • Thanks for the quick answer, 

     I am not sure if I am doing something wrong but I get an error: 

    Test-Path : Cannot bind argument to parameter 'Path' because it is null.
    At line:3 char:30
    +     ForEach-Object{ test-path <<<<  $_LocalPath }
        + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

    If I just run the command to verify there are some profiles with the name I am searching: 

    get-wmiobject win32_userprofile | Where-Object { $_.LocalPath -match '^C:\\users\\user[ds]a' } 
    I get the result confirming there are multiple folder with that naming (c:\users\userda* or C:\userssa*)


    
    Saturday, August 5, 2017 10:02 PM
  • Ah there was a missing dot on 
    $_.LocalPath


    get-wmiobject win32_userprofile | 
    	Where-Object { $_.LocalPath -match '^C:\\users\\user[ds]a' } | 
    	ForEach-Object{ test-path $_.LocalPath }

    Works great! Thank you!
    • Edited by nakamed Saturday, August 5, 2017 10:08 PM Oops
    Saturday, August 5, 2017 10:05 PM