1

I want to use a variable for the path, instead of typing it manually

Instead of this

#create a folder 
New-Item -Path 'D:\Test\New_folder' -ItemType Directory

to have something like this

$path='D:\Test'

#create a folder with a var

New-Item -Path '$path\New_folder' -ItemType Directory

But it is not working. I really appreciate any help you can provide.

1
  • 2
    Is't's not working because of the single-quotes you use. That way, $path will not get expanded. Either use double-quotes " or better, use the Join-Path cmdlet as in Join-Path -Path $path -ChildPath 'New_Folder' Commented Jul 16, 2022 at 9:48

1 Answer 1

1

You just have to use double quotes " to allow var to expend :

$yourPath='D:\Test'
New-Item -Path "$yourPath\New_folder" -ItemType Directory
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.