Creating files and directories in Windows Powershell
Creating files
Simplest way to create a file in Powershell is to use below command.
> notepad abc.txt
We can also create file using below commands.
- echo “xyz” > abc.txt : This command will create new file abc.txt if it does not exist. If it exists, contents will be overwritten.
- echo “xyz” >> abc.txt : This command will append the data to existing file.
- Get-Process > process.txt : This command will store the process information in the file “process.txt”. Note that we can store the output of any command in a file using this syntax.
Another way to create a file is by using below syntax.
dir | Out-File abc.txt
You can also use below command to create new file.
New-Item f1.txt -type file -force -value “You can also put this data in a file”
“Set-Content” command also allows you to create new file. Note that file is over-written if it already exists. In below example, file “f1.txt” is created with content as “hi how are you”.
Set-Content f1.txt “hi how are you”
Appending to file
You can use below syntax to append to file using ASCII encoding
dir | Out-File abc.txt -append -Encoding Ascii
You can also use below command to append data to file.
Add-Content <file-name> <data-to-be-appended>
Creating directories
To create a directory, you need to use below command with type as “directory”.
New-Item xyz -type directory
Recent Comments