We can create a file in PowerShell using the Out-File cmdlet from the Microsoft.PowerShell.Utility module.
Syntax:
Out-File
[-FilePath] <string>
[[-Encoding] <Encoding>]
[-Append]
[-Force]
[-NoClobber]
[-Width <int>]
[-NoNewline]
[-InputObject <psobject>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Example 1: One-Liner - Create a blank csv file
Out-File -FilePath "~/Desktop/data.csv"
Example 2: One-Liner - Create a Text file and write to it
PS> "Write to file!" | Out-File -FilePath "~/Desktop/message.txt" -Encoding utf8

Example 3: One-Liner - Create a file using PowerShell Script
$file = "~/Desktop/data.csv"
"1,Sam,2000" | Out-File -FilePath $file -Encoding utf8
Note: You can also make use of the New-Item cmdlet to create a empty file.
Example:
New-Item -Path myfile.txt -ItemType File
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!