PowerShell:如果"mkdir"命令存在文件alreadys,我该如何抑制错误?

Kit*_* Ho 10 powershell mkdir

考虑:

PS Y:\> mkdir  C:/dog


    Directory: C:\


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
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 26

-Force参数添加到命令中.

  • 使用 PowerShell 7 时我不喜欢“-Force”,因为当文件夹已经存在时,添加“-Force”会导致该文件夹的列表出现在命令的输出中。然而,您可以使用 @peter-mortensen 的答案,而不是使用“-Force”,即添加“-ErrorAction SilentlyContinue”。此选项不会导致输出中出现“嘈杂”文件夹列表。 (2认同)

Jac*_*kie 14

怎么样

mkdir C:\dog -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)