2

So, warning, this is probably a really newbie questions, so apologies in advance.

I'm starting to learn Powershell and one of the first things I want to do, is just make a directory & copy a file to it.

Now, if I use the following commands in a CMD window, they work perfectly.

mkdir %HOMEPATH%\test

cp test.txt %HOMEPATH%\test

However, when I put them into a .ps1 file and execute it, I get an error saying the directory could not be found etc (see below)

Copy-Item : Could not find a part of the path 'C:\Chef\windowsdevbox-master\%HOMEPATH%\.berkshelf'

Now, I got told that this is because I need to put CMD before each command. I ran this, with the CMD in front of each one and the error disappeared and instead I was presented with the "Home text" for CMD and the script finished.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

However, the folder was not created and the file was not copied over.

I just wondered what I need to do to get this to work.

3
  • Use powershell variables (instead of cmd.exe variables). See the output from get-childitem variable: in powershell. Commented Apr 23, 2015 at 19:55
  • So do you mean using variables such as "New-Item"? Or do you mean using variable such as "$fso.CreateFolder"? Commented Apr 23, 2015 at 20:02
  • I mean things like $HOME instead of %HOMEPATH% (though those aren't quite identical I think), etc. The problem with your sample commands is that %VAR% isn't a powershell variable. Commented Apr 23, 2015 at 20:03

2 Answers 2

5

In PowerShell mkdir is a built in function designed to emulate the same functionality, but using the built in cmdlet New-Item to do the underlying work.

cp is a straight up alias to PowerShell's Copy-Item cmdlet.

You don't need to precede these with cmd to make them work.

PowerShell does not accept the %VAR% syntax for environment variables though. Instead you would use the special $env variable, followed by a colon : followed by the variable name: $env:HOME.

mkdir $env:HOMEPATH\test

cp test.txt $env:HOMEPATH\test
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer :). I also tried this, just to learn and it worked as well.
3

%HOMEPATH% is not a PowerShell variable.

Environment variables are stored in the $env variable scope. You can access it with $env:homepath.

Here, I would use:

mkdir "${env:homepath}\test";
cp test.txt "${env:homepath}\test";

I might be inclined to use mkdir "${env:homedrive}${env:homepath}\test";, but I don't really know what you're trying to accomplish.

The curly braces here tell PowerShell that the entire contents are the variable name. The colon tends to confuse it, IMX, especially when you embed variables in strings.

Environment variables are special in PowerShell. They have their own provider. You can list them with Get-ChildItem env:, and manipulate them at the env: PSDrive.


Additional Note: Certain configurations might have unpredictable results because the strings might have too many backslashes or have backslashes in the wrong place. In this case, you might need to use Join-Path to combine the paths correctly.

Say:

$env:homedrive is 'U:'
$env:homepath is '\'
And the subfolder is '\test'

Then "${env:homepath}\test' is \\test, which looks like a UNC path. Instead you can use Join-Path ${env:homepath} '\test', which will then correctly create '\test'.

If you want to join three things, it's a bit complex:

Join-Path ${env:homedrive} (Join-Path ${env:homepath} '\test')

But that correctly creates 'U:\test' instead of 'U:\\test'.

You can also use the -Resolve option to convert a relative path to the absolute one.

1 Comment

Firstly, thanks for your explanation, it made perfect sense, i'll use this when going forward. Secondly, both commands worked perfectly. Thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.