PowerShell for Windows Admins

Aug 19 2017   7:52AM GMT

Create a directory

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

PowerShell enables you to work with the file system on your machine – one question that often comes up is how to create a directory.

When working interactively you can use md

PS> md c:\testf1


 Directory: C:\


 Mode LastWriteTime Length Name
 ---- ------------- ------ ----
 d----- 19/08/2017 14:24 testf1

md doesn’t look like a PowerShell command – more like an old style DOS command.

Its actually an alias for mkdir

PS> Get-Command md

CommandType Name Version Source
 ----------- ---- ------- ------
 Alias md –> mkdir

Which raises the question – what’s mkdir?

PS> Get-Command mkdir

CommandType Name Version Source
 ----------- ---- ------- ------
 Function mkdir

Its a function that PowerShell creates for you

Digging into the function

PS> Get-ChildItem -Path function:\mkdir | select -ExpandProperty Definition

<#
 .FORWARDHELPTARGETNAME New-Item
 .FORWARDHELPCATEGORY Cmdlet
 #>

[CmdletBinding(DefaultParameterSetName='pathSet',
 SupportsShouldProcess=$true,
 SupportsTransactions=$true,
 ConfirmImpact='Medium')]
 [OutputType([System.IO.DirectoryInfo])]
param(
 [Parameter(ParameterSetName='nameSet', Position=0, ValueFromPipelineByPropertyName=$true)]
 [Parameter(ParameterSetName='pathSet', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
 [System.String[]]
 ${Path},

[Parameter(ParameterSetName='nameSet', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
 [AllowNull()]
 [AllowEmptyString()]
 [System.String]
 ${Name},

[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
 [System.Object]
 ${Value},

[Switch]
 ${Force},

[Parameter(ValueFromPipelineByPropertyName=$true)]
 [System.Management.Automation.PSCredential]
 ${Credential}
 )

begin {

try {
 $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('New-Item', [System.Management.Automation.CommandTypes]
 ::Cmdlet)
 $scriptCmd = {& $wrappedCmd -Type Directory @PSBoundParameters }
 $steppablePipeline = $scriptCmd.GetSteppablePipeline()
 $steppablePipeline.Begin($PSCmdlet)
 } catch {
 throw
 }

}

shows that its based on New-Item

PS> New-Item -Path c:\ -Name testf2 -ItemType Directory


 Directory: C:\


 Mode LastWriteTime Length Name
 ---- ------------- ------ ----
 d----- 19/08/2017 14:32 testf2

The default for New-Item in the filesystem is to create a file so you need to use –ItemType Directory to create the folder.

If the folder you’re creating is a subfolder of a non-existent folder you can create the hierarchy is one go

PS> New-Item -Path c:\ -Name testf3\tests1 -ItemType Directory


 Directory: C:\testf3


 Mode LastWriteTime Length Name
 ---- ------------- ------ ----
 d----- 19/08/2017 14:33 tests1


 PS> Get-ChildItem -Path c:\testf3 -Recurse


 Directory: C:\testf3


 Mode LastWriteTime Length Name
 ---- ------------- ------ ----
 d----- 19/08/2017 14:33 tests1

This can get complicated if you try to nest too many levels so I recommend explicitly creating each level of your folder hierarchy. Its much easier to maintain and modify

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: