简体   繁体   English

Powershell正在其当前工作目录中创建目录

[英]Powershell is creating directories in its current working directory

I am facing a strange situation. 我面临一个奇怪的情况。 I am trying to create a directory structure in C: drive, but new directory structure gets created from where i am running my powershell script, I don't know why is this happening. 我正在尝试在C:驱动器中创建目录结构,但是从我运行Powershell脚本的位置创建了新的目录结构,我不知道为什么会这样。

$new_folder='C:\new_folder' 
new-item $new_folder -type directory

If I run the script from desktop , new folder gets created on desktop. 如果从桌面运行脚本,则会在桌面上创建新文件夹。 Can someone please help 有人可以帮忙吗

I can't replicate the problem to see if this works better but it may be worth a try: 我无法复制该问题,看看是否可以更好地工作,但值得一试:

    $new_foldername = 'new_folder'
    $new_folderpath = 'C:\' 
    New-Item -Name $new_foldername -Path $new_folderpath

It's a good practice to set ExecutionPolicy for your scripts, because when you are trying to access such resources (as C:\\ in your case), you'll not have to worry about it. 为脚本设置ExecutionPolicy是一个好习惯,因为当您尝试访问此类资源(如您的情况下为C:\\)时,您不必担心。

Also I had some issues with relative path access to files/folders in C:\\ and I had to use Resolve-Path in order to get script back-on-track. 另外,我对C:\\中的文件/文件夹的相对路径访问有一些问题,并且我不得不使用Resolve-Path才能使脚本回到正轨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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