Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: How can I suppress the error if file alreadys exists for "mkdir" command?

Tags:

Consider:

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 
like image 579
TheOneTeam Avatar asked Nov 08 '13 07:11

TheOneTeam


People also ask

How do I supress a PowerShell error?

If you need to suppress an error, you can use the ErrorAction switch to suppress an error for a single cmdlet or an ErrorAction preference variable to suppress errors globally.

Does mkdir work in PowerShell?

mkdir command used to create directory using cmd is also available as a function in PowerShell. PowerShell mkdir is a function defined in PowerShell to create directory and it's an alias of md command. PowerShell mkdir uses New-Item cmdlet to create directory and has the same syntax as PowerShell New-Item cmdlet.

How do you check file exist or not in PowerShell?

To use PowerShell to check if a file exists, you use the Test-Path Cmdlet. However, you can either enter the file path directly to the Test-Path command or save the file in a variable. Then use the variable in Test-Path.

How do I find the directory path in PowerShell?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.


Video Answer


2 Answers

Add the -Force parameter to the command.

like image 100
Keith Hill Avatar answered Oct 02 '22 00:10

Keith Hill


Use:

mkdir C:\dog -ErrorAction SilentlyContinue 
like image 44
Jackie Avatar answered Oct 02 '22 02:10

Jackie