Creating files and folders the easy way with PowerShell

File management is an essential part of the everyday routine as you’ll have to manage access to shared folders, back up files and folders regularly, and do the other standard operations on files and folders in your Windows system. The good news is you can use PowerShell to manage files easily. In fact, it can reduce the amount of manual work associated with file management. In this article, we’ll be focusing on how you can create files and folders in PowerShell

Creating a file

To create a new file, use the New cmdlet. In general, this cmdlet is used to create any type of object in PowerShell. All that you have to do is specify the type of object you want to create in this cmdlet.

Here is how you can create a file or folder in PowerShell using the New-Item command.

New-Item -Path '\\Shared\TestFolder\testfile1.txt' -ItemType File

The above command will create a new file called testfile1.txt. Here, the Itemtype is a file, so this will create a file for you and the contents will be empty. In other words, this command will create an empty text file for you.

Creating files after checking for the file name

Now if there was already a file with the same name, it will overwrite it. So, make sure you check for the file name before you create a file. Another option is to have a script that will automatically check if another file exists with the same name and will create a file only if there is no other file with that name.

An example of such a script is shown below.

$filenames = Get-ChildItem \\Shared\TestFolder\*.txt | select -expand fullname
$filenames -match "testfile1.txt"
If ($filenames -eq 'False') {
New -Item -Path '\\Shared\TestFolder\testfile1.txt' -ItemType File
}
else {exit}

The above script will check if a file with a similar name exists in the same directory and if so, it will exit. Otherwise, it will create a new file.

Overwriting an existing file

Earlier, we saw how to avoid overwriting an existing file. But sometimes, you may have to overwrite a file and you can do that using the Force switch parameter.

New-Item -Path '\\Shared\TestFolder\testfile1.txt' -ItemType File -Force

This command will forcefully create a file and overwrite the existing one because you’re effectively telling the system that you know what you’re doing and you don’t need any warnings or reminders about overwriting files. While this may seem like a good idea, there is always a possibility for errors and you could lose the data present in existing files. This is why it is not good programming to use this switch parameter and if you have to do so, exercise caution.

Writing data into new files

Most times, we don’t need an empty file as it makes little sense. Rather, you’d want a file with data and preferably with an option to add more content to it regularly.

The good news is PowerShell has a built-in command called Out-File to create a file and write data to it at the time of creation, provided you know what should go in.

$content = 'Hello world! This is the first line of my file.' | out-file -filepath C:\temp\testfile1.txt

This will create a file called textfile1.txt in the C:\Temp directory and it will contain your message.

files and folders powershell

Thus, these are the different ways to create a file including the considerations you should keep in mind.

Next, we’ll move on to creating folders or directories.

Creating a folder

There are four ways of creating a folder or directory in PowerShell and the choice depends on your programming needs.

Using native Windows PowerShell

Probably the easiest way to create a directory is to use the native Windows PowerShell command. We can use the same command used in file creation for folder creation too, and as you may have guessed, just change the value of ItemType:

New-Item -Path '\\Shared\TestFolder' -ItemType Directory

This command returns an object called DirectoryInfo that has the folder mode, last written time, and the length of the name.

You can even skip the Path parameter and can directly specify the path name and the result will be the same.

New-Item '\\Shared\TestFolder' - ItemType Directory

Make sure to include the ItemType value.

Using the .NET framework class

The next option is to create a folder using the system.io namespace and the CreateDirectory static method in the Directory class.

The command you can use is:

[system.io.directory]::CreateDirectory("\\Shared\TestFolder")

A word of caution here. This is a .NET framework class and it is not considered a good practice to use it within the Windows PowerShell environment. That said, the choice is up to you and this command will create a folder. Like the earlier option, it will return a DirectoryInfo object with all details about the newly created folder.

Using object

The third method is to use the Object method from the Scripting.FileSystemObject. Note that this is the same method used in VBScript and the advantages are that it is fast and relatively easy to use to create a directory.

$newdirectory = new-object -ComObject scripting.filesystemobject
$newdirectory.CreateFolder(“\\Shared\TestFolder”)

This command will return an object that contains the path and other information of the directory.

Using md function

The last option is to create a directory using the md function. Many people find this convenient as they’ve used it a lot in the command prompt to create various folders. Also, the syntax is fairly simple and you don’t have to mention the ItemType since md alone is enough to tell the system that you’re creating a folder.

So, the command is:

md \\Shared\TestFolder

This command also returns a DirectoryInfo object.

Thus, these are some of the different ways to create a folder in PowerShell. So, which of these four methods should you use to create a folder? Well, it depends on what works best for you. For example, if you’ve been using the command prompt to create folders, the md method is probably the easiest. On the other hand, if you want to keep all the code as PowerShell commands, using ItemType is the best choice. The other two also work well in different contexts. So, the choice is totally up to you and it will have no impact whatsoever in the folder creation process.

We hope this piece has clarified your doubts in creating files and folders in PowerShell.

Featured image: Pixabay

About The Author

1 thought on “Creating files and folders the easy way with PowerShell”

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top