This guide walks you through the steps to create new files and folders using PowerShell. Additionally, at the end I’ll show how to add text to files directly within PowerShell.
The New-Item
cmdlet will create new files or folders in PowerShell. By adjusting the -ItemType
parameter to File
or Directory
and specifying a name, you can quickly create the structures you need.
For further details and the latest examples, refer to the Microsoft Documentation on New-Item, which includes examples for creating PowerShell profiles, symbolic links, and more.
Contents:
> Creating New Folder in PowerShell
> Creating New Files in PowerShell
> Adding Text to Files in PowerShell
Creating New Folder in PowerShell
To create a new folder in PowerShell, use the New-Item
cmdlet with the -ItemType
parameter and set it to Folder
.
The command below creates a folder in the directory you are currently navigated to within the PowerShell Terminal. So, the new folder is being created in c:\users\pete\
# PowerShell create new folder New-Item -ItemType Directory -Name Test_Stuff
This command creates a folder named Test_Stuff
in that location:
Creating Files in PowerShell
Creating a file is just as simple as creating a folder. We just need to change the -ItemType
param to File
and replace <FileName>
with your desired name including the file extension.
For example, the following command creates a file named Test_File.txt
in the current working directory:
# PowerShell new file New-Item -ItemType File -Name Test_File.txt
Note: The only difference between creating a folder and a file is the value provided to the -ItemType
parameter (Directory
vs. File
).
Adding Text to Files in PowerShell
As a bonus, here’s how to add content to a file using the Add-Content
cmdlet. After adding text, you can verify the file’s contents using the Get-Content
cmdlet.
Here’s an example:
# PowerShell add text to file Add-Content .\Test_File.txt -Value 'Hi hi!... just a wee message here.' # PowerShell show file content Get-Content .\Test_File.txt
This approach is good for appending information to files directly within a script or terminal session. If doing development and coding, you’ll more likely be using a tool like Visual Studio Code for amending file contents.
For more advanced examples and best practices, don’t forget to check out the official Microsoft documentation which I’ve linked throughout this post.
Leave a Reply