How to Create a File using PowerShell on Mac


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
Create a File using PowerShell on Mac

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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap