PowerShell tutorial – Writing to text (log) file in file system – exploring Add-Content and Set-Content CMDLETs

Hi All,
Greetings for the day!!! Today new learning so sharing ! SHARING IS CARING 🙂
Also keen to include as much as PowerShell stuff on Knowledge-Junction – making PowerShell Hub – All PowerShell learning at one place
Details / Background / Use Case
- I am working on my SharePoint online project where I need to write some logs to text file from PowerShell script
- I need to write logs either .log file or .txt file
- We have PowerShell script to perform job
- Script suppose to read SharePoint list
- Based on details from SharePoint list, creating a modern communication site
- And few other properties like – adding owners, hiding site title and so on
- So need to maintain log for every functionality / operation – whether it succeeded or any error occurred
- If there is any error then writing exception in error log file
- Requirement is to maintain log files for success and exceptions for each site and details
We have detailed article on how to create a file (either empty or with initial content) – PowerShell tutorial – Creating text (log) file in file system – exploring New-Item CMDLET
In this article we will discuss how to append content to local file or overwrite the existing content
CMDLETs to write content to file
Set-Content
- Set-Content CMDLET replaces the existing content in a file
- 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>]
Example :
- Lets add content to knowledjejunction.txt file as
Set-Content c:\temp\knowledgejunction.txt 'Welcome to Knowledge Junction'
- If file is already not created, new file will be created

- Please note, this CMDLET replaces existing content in the file
- So if I run this CMDLET with different / new content it will replace existing content as
Set-Content c:\temp\knowledgejunction.txt 'Welcome again Knowledge Junction. This text will be overwritten in file.'
- Output will be
Welcome again Knowledge Junction

- If we want to append content to file then we need to use “Add-Content” PowerShell CMDLET
Add-Content
Add-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-NoNewline]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <string>]
[<CommonParameters>]
Add-Content
[-Value] <Object[]>
-LiteralPath <string[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-NoNewline]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <string>]
[<CommonParameters>]
- Add-Content CMDLET appends content to specified file
- Examples
- I am using “Add-Content” PowerShell CMDLET in my script as
- Maintaining two log files – one for success logs and one for errors
- Using value attribute for writing content in respective file
- $owner – UPN of user who will be set as owner of the given site – $siteURL
- In catch block where we need to log exceptions
try{
$owner = "sayyam@knowledgejunction1.onmicrosoft.com"
$siteURL = "https://knowledgejunction1.sharepoint.com/"
Set-SPOUser -Site $siteURL -IsSiteCollectionAdmin $true -LoginName $owner
Write-Host "Site collection admin added successfully"
Add-Content "c:\temp\successlogs.txt" -Value "Site collection admin added successfully - $owner"
}catch{
Add-Content "c:\temp\errorlogs.txt" -Value "Site collection admin adding failed - $owner $($_.Exception.Message)"
}#catch
- If we execute above code, we will get an exception since we are not connecting to our tenant and directly executing the CMDLET – Set-SPOUser
- errorlogs.txt file will be generated as
REFERENCES
Thanks for reading !!! HAVE a FANTASTIC LEARNING ! LIFE IS BEAUTIFUL 🙂
You must be logged in to post a comment.