PowerShell Folder/Directory Creation

Script:

$FldrName   = "NewFolder"
$FldrParent = "C:Temp-New"
$FldrPath   = $FldrParent + $FldrName
[IO.Directory]::CreateDirectory($FldrPath)

 

Output:

Mode                LastWriteTime     Length Name                                                                                                           
—-                ————-     —— —-                                                                                                           
d—-         11/9/2011  10:53 AM            NewFolder
                                                                                                      

Will create the whole path all along the specified folder location. It uses .net method Directory.CreateDirectory.

 

Script:

$FldrName   = "NewFolder"
$FldrParent = "C:Temp-New"
$FldrPath   = $FldrParent + $FldrName
$DirObj     = [IO.Directory]::CreateDirectory($FldrPath)

Write-Host "LISTING ALL _PROPERTIES_ ACCESSIBLE VIA CreateDirectory OBJECT METHOD"
ForEach ($attribute in ($DirObj | Get-Member -membertype properties | sort name )) {
       $atrrib = $attribute.Name
        "{0,-20}: {1}" -f $atrrib, $DirObj.$atrrib
}

 

Output:

LISTING ALL _PROPERTIES_ ACCESSIBLE VIA CreateDirectory OBJECT METHOD
Attributes          : Directory
BaseName            : NewFolder
CreationTime        : 11/9/2011 12:03:29 PM
CreationTimeUtc     : 11/9/2011 6:33:29 AM
Exists              : True
Extension           :
FullName            : C:Temp-NewNewFolder
LastAccessTime      : 11/9/2011 12:03:29 PM
LastAccessTimeUtc   : 11/9/2011 6:33:29 AM
LastWriteTime       : 11/9/2011 12:03:29 PM
LastWriteTimeUtc    : 11/9/2011 6:33:29 AM
Mode                : d—-
Name                : NewFolder
Parent              : Temp-New
Root                : C:

 

 

Script:

$FldrName = "NewFolder"
$FldrParent = "C:Temp-New"
$FldrPath = $FldrParent + $FldrName
$DirObj = [IO.Directory]::CreateDirectory($FldrPath)

Write-Host "LISTING ALL METHODS ACCESSIBLE VIA CreateDirectory OBJECT METHOD"
ForEach ($attribute in ($DirObj | Get-Member -membertype Method | sort name )) {
       $atrrib = $attribute.Name
        "{0,-20}: {1}" -f $atrrib, $DirObj.$atrrib
}

Output:

LISTING ALL METHODS ACCESSIBLE VIA CreateDirectory OBJECT METHOD
Create              : System.Void Create(System.Security.AccessControl.DirectorySecurity directorySecurity), System.Void Create()
CreateObjRef        : System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateSubdirectory  : System.IO.DirectoryInfo CreateSubdirectory(string path), System.IO.DirectoryInfo CreateSubdirectory(string path, System.Security.Access
Control.DirectorySecurity directorySecurity)
Delete              : System.Void Delete(), System.Void Delete(bool recursive)
Equals              : bool Equals(System.Object obj)
GetAccessControl    : System.Security.AccessControl.DirectorySecurity GetAccessControl(), System.Security.AccessControl.DirectorySecurity GetAccessControl(Sy
stem.Security.AccessControl.AccessControlSections includeSections)
GetDirectories      : System.IO.DirectoryInfo[] GetDirectories(), System.IO.DirectoryInfo[] GetDirectories(string searchPattern), System.IO.DirectoryInfo[] G
etDirectories(string searchPattern, System.IO.SearchOption searchOption)
GetFiles            : System.IO.FileInfo[] GetFiles(), System.IO.FileInfo[] GetFiles(string searchPattern), System.IO.FileInfo[] GetFiles(string searchPatter
n, System.IO.SearchOption searchOption)
GetFileSystemInfos  : System.IO.FileSystemInfo[] GetFileSystemInfos(
string searchPattern), System.IO.FileSystemInfo[] GetFileSystemInfos()
GetHashCode         : int GetHashCode()
GetLifetimeService  : System.Object GetLifetimeService()
GetObjectData       : System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
GetType             : type GetType()
InitializeLifetimeService: System.Object InitializeLifetimeService()
MoveTo              : System.Void MoveTo(string destDirName)
Refresh             : System.Void Refresh()
SetAccessControl    : System.Void SetAccessControl(System.Security.AccessControl.DirectorySecurity directorySecurity)
ToString            : string ToString()

Leave a Reply

Your email address will not be published. Required fields are marked *