PowerShell provides commands 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
PowerShell cmdlet, New-Item
is used to create files and folders. This command not only creates the files, but it also adds initial content to the files.
Through its -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 to pass the value “Directory
“.
Where to create the files or directories? That we need to instruct the path where to create files or folders through its -Path
parameter.
By using -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 do need to use -Name
parameter. For the files, in the command’s -Value
parameter, we can add the initial content.
Here is a simple example that creates a file and adds some content to it.
PS C:\PowerShell> New-Item -ItemType "File" -Path "." -Name "SampleTextFile.txt" -Value "File with some text"
Here is another example that creates a folder.
Notice that, -Path
parameter includes the directory name also, this way we can avoid using -Name
parameters.
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 use to create registry entries. We will discuss this in our next article(s).
See also, “PowerShell – Tee-Object cmdlet – Redirect output to two directions“.
[..] David
One thought on “PowerShell – How to create files and folders?”