EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Write to File
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

PowerShell Write to File

PowerShell Write to File

Introduction to PowerShell Write to File

The following article provides an outline for PowerShell Write to File. There are various scenarios in which the output generated needs to be written to a file. For example, it can be for documentation purposes, error logging or for debugging purposes. There are multiple ways in which output can be written to a file in PowerShell, and there are cmdlets for generating various types of files such as text, csv, xml or html files.

Different Cmdlet

Given below are the different cmdlets, their parameters and usage in detail, along with appropriate examples.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

1. Out-File

This is a basic cmdlet which sends the output to a file of the user’s choice. By default, it uses the inbuilt formatting mechanism to generate the output file. The output is written in the same format as to how it is displayed in the console.

Syntax:

Out-File [-FilePath] <string> [[-Encoding] <Encoding>] [-Append] [-Force] [-NoClobber] [-Width <int>] [-NoNewline] [-InputObject <psobject>] [-WhatIf] [-Confirm] [<CommonParameters>]

Parameters:

  • -Append: This adds the output to the existing file. The data type of this parameter is a switch. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Confirm: This ensures that user confirmation is received before the cmdlet is run. The data type of the parameter is a switch. Cf is the alias of this parameter. False is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Encoding: This denotes the encoding mechanism that should be used by PowerShell while generating the output file. The various types of encoding are as follows: ascii, utf32, utf8BOM, utf8NoBOM, utf7, utf8, oem and Unicode. The default value is utf8NoBOM. The datatype of this parameter is Encoding. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -FilePath: This represents the location of the output file. The data type of this parameter is a string. Its alias is the path. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Force: This parameter is used to overwrite an existing read-only file. A switch is the data type of this parameter. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -InputObject: It represents the objects that need to be sent to the output file. It can be a variable which holds objects or an expression whose result is a collection of objects. The data type of this parameter is PSObject. None is the default value. This parameter recognizes pipeline input, but wild card characters are not permitted.
  • -LiteralPath: This also represents the location of the output file. A string is the data type of this parameter, and its aliases are PSPath and LP. None is the default value. This parameter recognizes pipeline input, but wild card characters are not permitted.
  • -NoClobber: This is used for preventing overwriting of files. If the output file exists, a message is displayed. If this is not used, then the file is overwritten. The data type of this parameter is a switch. The default value is none. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -NoNewLine: This is used to prevent new lines from getting inserted into the output. No newlines are added. The data type of this parameter is a switch. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Width: This represents how many characters should be there in each line of output. If the length exceeds this limit, those characters are truncated. The default limit is 80. Therefore, the data type of this parameter is 80. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.

Example:

Code:

Write-Host "Demo of out file cmdlet" -ForegroundColor Green
Write-Host "writing to a text file" -ForegroundColor Green
for($i=0;$i -le 10; $i++)
{
$out="Added" | Out-File -FilePath C:\vignesh\demo.txt -Append
}
Write-Host "Demo of limit width to 2"
for($i=10000;$i -le 10010; $i++)
{
$out="Added" | Out-File -FilePath C:\vignesh\demo1.txt -Append -Width 2
}
Write-Host "Displaying the output from the files" -ForegroundColor Green
Get-Content -Path "C:\vignesh\demo.txt"
Get-Content -Path "C:\vignesh\demo1.txt"

Output:

PowerShell Write to File 1

PowerShell Write to File 2

2. Set-Content

This cmdlet is also like add-content, except that it overwrites the file’s existing contents and won’t append.

Syntax:

Set-Content [-Path] <string[]> [-Value] <Object[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-NoNewline] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParameters>]

Parameters:

  • -Confirm: This ensures that user confirmation is received before the cmdlet is run. The data type of the parameter is a switch. Cf is the alias of this parameter. False is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Encoding: This denotes the encoding mechanism that should be used by PowerShell while generating the output file. The various types of encoding are as follows: ascii, utf32, utf8BOM, utf8NoBOM, utf7, utf8, oem and Unicode. The default value is utf8NoBOM. The datatype of this parameter is Encoding Pipeline input is not recognized by this parameter also wild card characters are not permitted.
  • -FilePath: This represents the location of the output file. The data type of this parameter is a string. Its alias is the path. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.
  • -Force: This parameter is used to overwrite an existing read-only file. A switch is the data type of this parameter. None is the default value. This parameter does not recognize pipeline input; also, wild card characters are not permitted.

Example:

Code:

Write-Host "Demo of set content cmdlet" -ForegroundColor Green
Write-Host "writing to a text file" -ForegroundColor Green
for($i=0;$i -le 10; $i++)
{
$out="Added" | Set-Content -Path C:\vignesh\demo.txt
}
Write-Host "Displaying the output from the files" -ForegroundColor Green
Get-Content -Path "C:\vignesh\demo.txt"
Write-Host "Rewriting the contents of the file"
$out="newly added" | Set-Content -Path C:\vignesh\demo.txt
Get-Content -Path "C:\vignesh\demo.txt"

Output:

Set-Content

Conclusion – PowerShell Write to File

Thus, the article explained in detail about how writing to a file is done in PowerShell. It explained the various methods, their parameters and syntax, along with appropriate examples. This can be learnt in detail by writing and practicing sample scripts.

Recommended Articles

This is a guide to PowerShell Write to File. Here we discuss the introduction and different cmdlet for a better understanding. You may also have a look at the following articles to learn more –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more