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 Set-Content
Secondary Sidebar
PowerShell Tutorial
  • 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
  • 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
  • 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

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Set-Content

By Priya PedamkarPriya Pedamkar

PowerShell Set-Content

Introduction to PowerShell Set-Content

In the computer many times we needed to write some contents on the file, here contents can be a string or any numerical values. So to do so we can open a file and write our required contents. But PowerShell gives us the power to write our contents on the file without opening the file. That means we can write content by command line. So Set-Content is the command which helps us to write our contents on any file system. If you are aware of Linux then in Linux it has a command called echo to write contents on the file. PowerShell Set-Content is not limited to writing content on the file it can append new contents, replace older contents, etc. Real-Time use of this command is, suppose you have a very large file and you wanted to replace some words in the whole file then you can directly write command of one line and words will be replaced.

Syntax and Parameters of PowerShell Set-Content

Following are the syntax of PowerShell Set-Content:

Syntax #1: Syntax without -Literalpath

Set-Content
[-Path] <array of strings are allowed in path>
[-Value] <Objects will be used here>
[-PassThru(return current content object which is holding it)] [-Filter <filter string for which we are going to write for filter conditions>] [-Include <any string that need to include in this operation>] [-Exclude <any string that need to exclude in this operation>] [-Force(file is busy or read only still it will allow to write contents)] [-WhatIf(describe what will happen is command will execute)] [-Confirm(asking before execution of command)] [-NoNewline(without new line and space concatenation of string)] [-Encoding <here we can define types of contents>] [<CommonParameters>]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax #2: Syntax with -LiteralPath

Set-Content
[-Value] <Objects will be used here>
-LiteralPath <mention exact location>
[-PassThru(return current content object which is holding it)] [-Include <any string that need to include in this operation>] [-Exclude <any string that need to exclude in this operation>] [-Force(file is busy or read only still it will allow to write contents)] [-Credential ] [-WhatIf(describe what will happen is command will execute)] [-Confirm(asking before execution of command)] [-NoNewline(without new line and space concatenation of string)] [-Encoding <here we can define types of contents>] [<CommonParameters>]

Parameters:

Following are the top 12 parameters of PowerShell Set-Content:

  1. -Confirm: Display a confirmation message before executing the Set-Content command. These commands are always advisable to use, to avoid any big mistakes.
  2. -Encoding: It allows us to mention the type of encoding for the target file. It accepts ASCII, BigEndingUnicode, OEM, Unicode, UTF7, UTF8, etc.
  3. -Exclude: At the time of setting content to target file contents, specific strings can be excluded. We can use wildcard characters with (*).
  4. -Filter: You can define filter before content writing. It allowed wildcard also(*).
  5. -Force: Force commands are used when we need to write something any target file even the file is the read-only condition.
  6. -Include: Here we can specify string needs to include in Set-Content operation, In this operation, we can also use a wildcard (*).                              
  7. -LiteralPath: This command will be used if you have an exact path because it does not allow wildcard or matching attributes.
  8. -NoNewline: We can directly concat string without any new line, without any space. Basically new line will be added at the end of the output string.
  9. -PassThru: Display the object which holds the contents. It will not return anything by default.
  10. -Path: It defines the item’s path of receiving content. You can use wildcard characters here.
  11. -WhatIf: It shows details of command, or simply consequences after the execution of the program. If you want to see the output or purpose of the message then you can use it.
  12. -Value: Any new content which we are going to write on the target file.                                         

Examples to Implement PowerShell Remove-Item

Below are the examples to implement PowerShell Remove-Item:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

Example #1

This is a very simple example for Set-Content, Here we are adding a string to an existing file. We can see in the example that we are passing -Value as “Hello, Friend my name is Ranjan”.It simply appends this string value.

Example of practical execution can be seen in the image given below.

Set-Content -Path ./test.txt -Value “Hello ,friends my name is Ranjan”
Get-Content -Path ./test.txt

Output:

Example 1-1

Example #2

Here we are going to write content to the non-existing file, which means we will create a new file and then append the string message. Please refer to the image and example given below. You can try the same command with the existing file to see the difference in output.

Set-Content -Path .\todatsDate.txt -Value (Get-Date)
Get-Content -Path ./todatsDate.txt

Output:

Powershell Set-Content 1-2

Example #3

Now let’s take an example of replacing the string with another. In the example below you can see first we tried to replace “Ranjan” with “Ajay” and it failed as our string contains “Ranjan” not “Ranjan”, which means case sensitive. And again when we tried to replace “Ranjan” with “Ajay”, it worked as “Ranjan” is there in our existing string. Please refer to the example and screen given below.

(Get-Content ./test.txt).replace('Ranjan', 'ajay') | Set-Content ./test.txt

Output:

Powershell Set-Content 1-3

Example #4

We learned how to append string on a file in PowerShell, now let’s take an example where we want to append multiple strings to multiple files. In the below example we are appending ‘Hello friend this content will be on all three files’ to all files with name containing the test. In the directories, there were three file test1.txt, test2.txt, test3.txt, and all the files got the same string. Please refer to the example screen below.

Set-Content -Path .\test*.txt -Value 'Hello friend this content will be on all three file'
Get-Content -Path .\test*.txt

Output:

how to append string on a file

Conclusion

I am hoping that this tutorial was good enough to give a brief to Set-Content. In this tutorial, we understand how we can add replace the string on any target file or files. It reduces our effort to do too many manual works to perform any repetitive work on a file system by just writing a single command to replace or write content on one or many files on one execution.

Recommended Articles

This is a guide to PowerShell Set-Content. Here we discuss the syntax, parameters, and different examples to implement Remove-Item. You may also look at the following articles to learn more-

  1. Comparison Operators in PowerShell
  2. PowerShell vs Command Prompt
  3. How To Install PowerShell
  4. PowerShell Interview Questions
  5. Complete Guide to PowerShell Format Table
  6. Overview and Top Different PowerShell Tools
  7. PowerShell Get-Location | Syntax | Parameters | Implementation
  8. PowerShell Get-Date | Examples
  9. Guide to PowerShell Get-Content
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
Data Visualization Training (15 Courses, 5+ Projects)4.8
0 Shares
Share
Tweet
Share
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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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