How to create files and folders easily with Powershell
File management is an essential part of everyday work. You will have to manage access to shared folders, back up them regularly, as well as perform other standard operations on files and folders in the Windows system. The good news is that you can use PowerShell to manage files easily.
In fact, this tool can reduce the amount of manual work involved in file management. In this article, TipsMake will focus on how you can create files and folders in PowerShell.
How to create files and folders using Powershell?
- Create a file
- Create the file after double checking the file name
- Overwrite an existing file
- Write data to new file
- Create a directory
- Use original Windows PowerShell
- Using the .NET framework class
- Use the Object
- Use the md function
Create a file
To create a new file, use the New cmdlet. Generally, this cmdlet is used to create any type of object in PowerShell. All you have to do is specify the type of object you want to create in this cmdlet.
This is how you can create files or folders in PowerShell with the New-Item command .
New-Item -Path 'SharedTestFoldertestfile1.txt' - ItemType File
The above command will create a new file called testfile1.txt. Here, Itemtype is a file, so this command will create one for you and leave the content empty. In other words, this command will create a blank text file for you.
Create the file after double checking the file name
Now, if you already have a file with the same name, it will be overwritten. So make sure you double check the file name before creating. Another option is to have a script automatically check if another file exists with the same name and will only create the file if no other file is named there.
An example of such a script is shown below.
$filenames = Get-ChildItem SharedTestFolder*.txt | select -expand fullname $filenames -match "testfile1.txt" If ($filenames -eq 'False') { New -Item -Path 'SharedTestFoldertestfile1.txt' - ItemType File } else {exit}
The above script will check if a file with the same name exists in the same directory and if it does, it will exit. If not, a new file will be created.
Overwrite an existing file
Earlier, the article showed you how to avoid overwriting an existing file. But sometimes, you have to overwrite a file and can do it using the Force parameter .
New-Item -Path 'SharedTestFoldertestfile1.txt' - ItemType File -Force
This command will force to create a file and overwrite the existing file, because you are telling the system that you know what you do and do not need any warning or prompt about the overwritten file. Although this sounds like a good idea, there's always a chance that you'll get an error and you could lose the data contained in existing files. This is why it is not recommended to abuse this conversion parameter, and if you must do so, be very careful.
Write data to new file
In most cases, you don't need an empty file because it's pointless. Instead, you want a file containing data and it is best to have the option to add content to it regularly.
The good news is that PowerShell has an integrated command called Out-File to create files and write data to them at the time of creation.
$content = 'Hello world! This is the first line of my file.' | out-file -filepath C:temptestfile1.txt
This will create a file named textfile1.txt in the C: Temp directory and it will contain your message.
Write data to new fileSo there are different ways to create a file that includes the considerations you should keep in mind.
Next, we will move on to the directory creation section.
Create a directory
There are 4 ways to create folders in PowerShell and the choice depends on your programming needs.
Use original Windows PowerShell
Perhaps the easiest way to create a directory is to use the original Windows PowerShell command. You can also use the same command used in creating files to create folders and, as you anticipated, simply change the value of ItemType :
New-Item -Path 'SharedTestFolder' - ItemType Directory
This command returns an object called DirectoryInfo that has the directory mode, the last recording time, and the length of the name.
You can even omit the Path parameter and can specify the path name directly. The result will be the same.
New-Item 'SharedTestFolder' - ItemType Directory
Be sure to include the ItemType value .
Using the .NET framework class
The next option is to create a directory using the system.io namespace and the static method CreatDirectory in the Directory class .
The command you can use is:
[system.io.directory]::CreateDirectory("SharedTestFolder")
Warning : This is the .NET framework class and it is not considered a good way to use it in a Windows PowerShell environment. The choice is up to you and this command will help create a directory. Like the previous option, the command will return a DirectoryInfo object with all directory details.
Use the Object
The third way is to use the Object method from Scripting.FileSystemObject. Note that this is the same method used in VBScript, which has the advantage of being quick and relatively easy to use to create directories.
$newdirectory = new-object -ComObject scripting.filesystemobject $newdirectory.CreateFolder('SharedTestFolder')
This command will return an object containing the directory path and other information.
Use the md function
The last option is to create a directory using the md function . Many people find this convenient because they have used it a lot in the Command Prompt to create different folders. Also, the syntax is quite simple and you don't need to mention ItemType , because md alone is enough to tell the system that you are already creating the directory.
The command to use is:
md SharedTestFolder
This command also returns a DirectoryInfo object .
You should read it
- About PowerShell
- Use PowerShell to download any file on Windows 10
- How to read text files in Powershell quickly and easily
- How to install PowerShell 7.0 in Windows 10/8/7
- How to create and run a PowerShell script file on Windows 10
- How to compress and decompress files with PowerShell on Windows
- Use GUI to create PowerShell scripting using PowerGUI
- What is PowerShell Basic commands in PowerShell
- Use PowerShell to create EventLog
- PowerShell command in Windows
- How to use PowerShell's default parameter to change the command behavior
- How to check PowerShell version in Windows 10
Maybe you are interested
9 ways to fix the error of not being able to save files as JPEG or PNG in Photoshop
Download standard and beautiful Word file experience initiative cover template
How to use the file search command on Windows, find saved files
How to split sheet into multiple separate Excel files
How to merge PDF files, merge and join multiple PDF files into a single file
How to clean up iPhone junk, delete junk files to free up iPhone storage