简体   繁体   中英

Powershell issue when executing .ps1 script

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.

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

%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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM