PowerShell provides command(s) to create files and folders. Through this article, we are going to discuss how to create files and folders using PowerShell command(s).
New-Item cmdlet – Create files and folders
New-Item is used to create files and folders. This command not only creates the files, it also add initial content to the files.
-ItemType parameter we need to instruct to create a file or folder. To create a file we need to pass the value “
File” and to create a folder we need pass the value “
Where to create the files or directories? That we need to instruct the path where to creates files or folders through its
-Name parameter, we can specify the name of the file or folder we are going to create, in the specified location. If we provide the name of the file or folder in
-Path parameter, then we no need to use
-Name parameter. For the files, in command’s
-Value parameter, we can add the initial content.
Here is a simple example which creates a file and add some content to it.
PS C:\PowerShell> New-Item -ItemType "File" -Path "." -Name "SampleTextFile.txt" -Value "File with some text"
Here is another example which creates a folder.
-Path parameter includes the directory name also, this way we can avoid to use
PS C:\PowerShell> New-Item -ItemType "Directory" -Path ".\SubDirectroy"
New-Item is useful to create different types of items and depends on the location of the item. Through this article we have discussed the creation of files and folders using this command. But, this command can also useful to create registry entries. We will discuss about this in our next article(s).