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
?
Split() function. The . Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks.
The rules for converting any value to type byte, int, or long are as follows: The bool value False is converted to zero; the bool value True is converted to 1. A char type value whose value can be represented in the destination type has that value; otherwise, the conversion is in error.
Use [int] to Convert String to Integer in PowerShell The data type of $a is an integer . But when you enclose the value with " " , the data type will become string . To convert such string data type to integer, you can use [int] as shown below.
Okay, the answer seems to be Get-Item
:
$dirAsStr = '.\Documents'
$dirAsDir = Get-Item $dirAsStr
echo $dirAsDir.FullName
Works!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With