Four ways to use PowerShell to create Folders


To totally unlock this section you need to Log-in


Login

It is possible to use the Directory .NET Framework class from the system.io namespace. To use the Directory class to create a new folder, use the CreateDirectory static method and supply a path that points to the location where the new folder is to reside. This technique is shown here.

[system.io.directory]::CreateDirectory("C:\test")

When the command runs, it returns a DirectoryInfo class. The command and its associated output are shown in the image that follows.

Four ways to use PowerShell to create Folders

Another way to create a folder is to use the Scripting.FileSystemObject object from within Windows PowerShell. This is the same object that VBScript and other scripting languages use to work with the file system. It is extremely fast, and relatively easy to use.

After it is created, Scripting.FilesystemObject exposes a CreateFolder method. The CreateFolder method accepts a string that represents the path to create the folder. An object returns, which contains the path and other information about the newly created folder. An example of using this object is shown here.

$fso = new-object -ComObject scripting.filesystemobject

$fso.CreateFolder("C:\test1")

This command and its associated output are shown in the following image.

Four ways to use PowerShell to create Folders

It is also possible to use native Windows PowerShell commands to create directories. There are actually two ways to do this in Windows PowerShell. The first way is to use the New-Item cmdlet. This technique is shown here.

New-Item -Path c:\test3 -ItemType directory

The command and the output from the command are shown here.

Four ways to use PowerShell to create Folders

Compare the output from this command with the output from the previous .NET command. The output is identical because the New-Item cmdlet and the [system.io.directory]::CreateDirectory command return a DirectoryInfo object. It is possible to shorten the New-Item command a bit by leaving out the Path parameter name, and only supplying the path as a string with the ItemType. This revised command is shown here.

New-Item c:\test4 -ItemType directory

Some might complain that in the old-fashioned command interpreter, cmd, it was easier to create a directory because all they needed to type was md –– and typing md is certainly easier than typing New-Item.

The previous complaint leads to the fourth way to create a directory (folder) by using Windows PowerShell. This is to use the md function. The thing that is a bit confusing, is that when you use Help on the md function, it returns Help from the New-Item cmdlet—and that is not entirely correct because md uses the New-Item cmdlet, but it is not an alias for the New-Item cmdlet.

The advantage of using the md function is that it already knows you are going to create a directory; and therefore, you can leave off the ItemType parameter and the argument to that parameter. Here is an example of using the md function.

md c:\test5

The command and its associated output are shown here.

Four ways to use PowerShell to create Folders

You can see from the image above that the md function also returns a DirectoryInfo object.