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
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.
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.
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.
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.
Add the -Force
parameter to the command.
Use:
mkdir C:\dog -ErrorAction SilentlyContinue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With