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
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Powershell,
- PowerShell: How to Check if a Module is Already Installed
- PowerShell: How to Write Output to a File
- PowerShell: Steps to Connect to connect to Exchange Online
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- PowerShell Traditional For Loop Example
- How to Run PowerShell Script as Administrator (Elevate)
- Update Powershell Using Command Line
- How to delete a file using PowerShell [Windows/macOS]
- PowerShell: Grep Command Alternative - Select-String
- PowerShell Concatenate String Examples
- How to add Sleep to PowerShell Script
- Connect Azure AD (Active Directory) for PowerShell
- How to Open PowerShell on Mac?
- List of PowerShell Function Commands for Mac
- How to Connect to Azure AD Account using PowerShell on Mac
- Download Google Chrome setup exe file using PowerShell
- Set Environment Variable in PowerShell for Mac
- How to switch to Powershell on Mac Terminal
- How to add sleep in Powershell Script
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
- How to Identify installed PowerShell version
- How to check PowerShell version
- How to install PowerShell on Mac using Brew
- PowerShell Switch Statement with Examples
- PowerShell ISE Alternative for Mac
More Posts:
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml - SharePoint
- Error code - 7: There's a more permanent way to sign in to Microsoft Teams - Teams
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text
- Convert Multidimensional Array toString In Java - Java
- PHP header location function not called - PHP
- Show battery percentage on MacBook Menu Bar [Ventura 13] - MacOS
- Test internet speed using macOS Terminal command - MacOS
- iPhone Message: A new iOS update is now available. Please update from the iOS 14 beta. - Apple