Convert string to directory object in PowerShell

So, the simple way for get path/full path from string type variable, that always works to me:

(Resolve-Path $some_string_var)

Set-Variable -Name "str_path_" -Value "G:\SO_en-EN\Q33281463\Q33281463.ps1"

$fullpath = (Resolve-Path $some_string_var) ; $fullpath


Okay, the answer seems to be Get-Item:

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

Works!


You can use .Net class System.IO.FileInfo or System.IO.DirectoryInfo. This will work even if directory does not exist:

$c = [System.IO.DirectoryInfo]"C:\notexistentdir"
$c.FullName

It will even work with a file:

$c = [System.IO.DirectoryInfo]"C:\some\file.txt"
$c.Extension

So to check if it is really a directory use:

$c.Attributes.HasFlag([System.IO.FileAttributes]::Directory)

There's an example with System.IO.FileInfo in the comment below.