3

This is a question of understanding.

I construct a Directory Structure using a string Variable $path. I append the name of the Directory I want to create, this works as expected. When the path is completed I need the complete Directory as System.IO.DirectoryInfo but assigning the Path in this way $Path = Get-Item $Path results in a string type.

$path = "c:\dir1"
If(-Not (Test-Path $path))New-Item -ItemType Directory -Path $path
$path = $path + "\dir2"
If(-Not (Test-Path $path))New-Item -ItemType Directory -Path $path

# Assigned to same variable
$path = Get-Item $path
echo $path.GetType() # = string

# assigned to different variable
$p_a_t_h = Get-Item $path
echo $p_a_t_h.GetType() # = System.IO.DirectoryInfo

# solution but not understood the behavior
[System.IO.DirectoryInfo]$path = Get-Item $path
echo $path.GetType() # = System.IO.DirectoryInfo

It took hours to find out this behavior and I couldn't find any documentation why this is - maybe because I don't know what to search for.

It is clear, that for appending something to a variable, the type of the variable is relevant, but a $path = ... is a "new" assignement and should have the type of the assigned value - at least in my eyes. In the languages I used so far a variable becomes the type of its value and is not converted to a type the variable had earlier or I define the type of a variable and get an error if assigned with wrong type.

Where is the error in my logic?

6
  • learn.microsoft.com/en-us/powershell/module/… The Path parameter accepts a string input not a System.IO.DirecetoryInfo object Commented Jan 5, 2018 at 16:16
  • Why do you need/want to reassign the same variable when using separate variables (of the type you require) would make your life much easier? Commented Jan 5, 2018 at 16:19
  • @James it is not too important to have the same variable reassigned - for me it is just the same information with a different tpye. It is just a little script and i didn't think too much about it until i came across this problem. The point is not to solve the problem - i did already in many ways, but to understand why this is the case. Commented Jan 5, 2018 at 16:31
  • @user2734259 i know this and that's the reason why i tried to reassign [System.IO.DirectoryInfo]$path after i have created the Directorystructure using [string]$path Commented Jan 5, 2018 at 16:36
  • @user2734259 I've changed my script according to the info from briantist and found out that the -Path parameter accepts a [System.IO.DirecetoryInfo] object - no need to use [System.IO.DirecetoryInfo]$path.FullName Commented Jan 5, 2018 at 17:37

1 Answer 1

2

I think that somewhere in your code you did a left-side cast (on the variable, not the value) to [String], just like you did later in your sample with [System.IO.DirectoryInfo]$path.

The most common way that this happens: Parameters.

Is this taken from a function? Like:

function Invoke-MyThing {
param([String]$Path)

}

Why that matters

When you put the type on the variable, all values assigned to that variable receive that cast.

[String]$Value = 'Hello'
$Value.GetType()

$Value = 3.141
$Value.GetType()

Casting the value only affects that one value:

$V2 = 5
$V2.GetType()

$V2 = [String]9
$V2.GetType()

$V2 = 45
$V2.GetType()

So, remove your previous variable-side cast, or if it's a parameter, just use a different local variable.

Better yet, if it's a parameter, you could make it of type [System.IO.DirectoryInfo] instead.. then it would accept that directly, or even accept a string. You just have to rework your code a little bit to deal with it.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it was a parameter. Do i understand this correctly, if i don't cast the variable anywhere it will be of the type of the value i assigned and will change when reassigned? When i use [System.IO.DirectoryInfo] and assign a [string] to it, can i still append/reassign a new [string] or do i need the -Name or -FullName Attributes? $path.name = $path.name + "\somedit" or $path = $path.name + "\somedir"
@HolgerZ if the variable has no cast assigned, you can reassign new values of new types and it will accept those types. If the variable has a cast, that cast will apply to the value on assignment (following all the same rules of any cast).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.