添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 11/7/2013 10:59 PM dog PS Y:\> mkdir C:/dog New-Item : Item with specified name C:\dog already exists. At line:38 char:24 + $scriptCmd = {& <<<< $wrappedCmd -Type Directory @PSBoundParameters } + CategoryInfo : ResourceExists: (C:\dog:String) [New-Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand Is there any reason why -Force wouldn't work? In my OctopusDeploy script, this was generated. $tempfolder was a UNC path. screencast.com/t/iB5rlt4Ae1 Ross Presser May 6, 2016 at 15:12 Never mind ... it's because my OctopusDeploy script was running as SYSTEM, which has no rights to the UNC path in question. Ross Presser May 6, 2016 at 15:21 I don't like '-Force' when using PowerShell 7, because when the folder already exists, adding '-Force' causes a listing of the folder to appear on the output of the command. Whereas, instead of using '-Force' you can use @peter-mortensen 's answer, which is to add '-ErrorAction SilentlyContinue'. This option causes no 'noisy' folder listing to appear on the output. Darrin Nov 9, 2022 at 19:32 This ignores all errors. E.g. if you were not authorized to create the directory, you now have no directory created and your script continues. Ignoring only the error when the directory exists means there IS a dog directory when your script continues. Anders Forsgren Feb 22, 2022 at 13:09 This is the BEST answer, in my opinion, because it does not cause a directory listing to appear on the output like the '-Force' argument. Darrin Nov 9, 2022 at 19:29

It is a best practice to not supress error messages (unless you have a valid reason). Check if the directory exists instead of just trying to create one. If it does, maybe you need to remove its contents or pick another a name? Like so,

if (-not (test-path "c:\foobar") ) {
    write-host "c:\foobar doesn't exist, creating it"
    md 'c:\foobar'|out-null
} else {
    write-host "c:\foobar exists, no need to create it"
                In my opinion, that is WAY too much typing/effort required, when all you want is for the attempt to create the directory to be made, but WITHOUT complaining if it already exists.
– Darrin
                Nov 9, 2022 at 19:26

I just wanted to add that while suppressing the error is usually not best practice as you say, the command -Force runs much faster than checking if it exists prior.

Where D:\ is a RAM disk:

Measure-Command {new-item "D:\NewFolder\NewSubFolder" -ItemType Directory -force}

First run (creating folder object): 5 ms

Second run (after folder exists): 1 ms

Measure-Command {if (-not (test-path "D:\NewFolder\NewSubFolder") ) {
write-host "Directory doesnt exist, creating it"
md "D:\NewFolder\NewSubFolde"|out-null} else {
write-host "Directory exists, no need to create it"}}

First run (creating folder object): 54 ms

Second run (after folder exists): 15 ms

Thank you Peter for cleaning up my post! You are the man!

-Force is a parameter of New-Item, sorry that was very much the wrong terminology! Taking out the Write-Host in the above command does speed things up big time! Poor example, but the result is still similar. Sorry for the confusion guys! – Jordan Sep 20, 2018 at 20:42

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.