Search code examples
powershellpowershell-4.0

Convert string to directory object in PowerShell


I read strings line by line from a file (using Get-Content and a foreach loop), I want to convert those strings to directory objects (so that I can access properties like .FullName). How to easily convert from string to directory?

With files it is easy: $myFileAsFile = $myFileAsStr | dir $_, however, how to obtain my goal for a $directoryAsString?


Solution

  • Okay, the answer seems to be Get-Item:

    $dirAsStr = '.\Documents'
    $dirAsDir = Get-Item $dirAsStr
    echo $dirAsDir.FullName
    

    Works!