Small Tips and Tricks – PowerShell – how to read content from text file

reading text file content in PowerShell
reading text file content in PowerShell

Hi All,

Greetings for the day!!!

Today new small but useful finding

Background / Use Case

In one of our PowerShell script we need to read the credentials (Username / Password OR Client APP ID / Client Secret Key OR Certificate password) from local text file

Details

  • Get-Content CMDLET is used to read the text file from given path
  • Get-Content CMDLET reads file line by line
  • Example – Following is the content in sample text file – “credentials.txt”
    • Username
    • Password
$creds = Get-Content -Path <Path of text file>

$creds = Get-Content -Path “credentials.txt”

  • By default, Get-Content reads every line in a text file and creates an array as its output with each line of the text as an element in that array.
  • So, output of above command will be

Username
Password

Syntax

Get-Content
   [-ReadCount <Int64>]
   [-TotalCount <Int64>]
   [-Tail <Int32>]
   [-Path] <String[]>
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Force]
   [-Credential <PSCredential>]
   [-Delimiter <String>]
   [-Wait]
   [-Raw]
   [-Encoding <Encoding>]
   [-AsByteStream]
   [-Stream <String>]
   [<CommonParameters>]

Get-Content
   [-ReadCount <Int64>]
   [-TotalCount <Int64>]
   [-Tail <Int32>]
   -LiteralPath <String[]>
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Force]
   [-Credential <PSCredential>]
   [-Delimiter <String>]
   [-Wait]
   [-Raw]
   [-Encoding <Encoding>]
   [-AsByteStream]
   [-Stream <String>]
   [<CommonParameters>]

Simple example

  • Lets have simple text file – “credentials.txt” with following content

user name – prasham@knowledgejunction1.onmicrosoft.com

password – LIFE IS BEAUTIFUL

fig : Text file with two lines
fig : Text file with two lines

Script to read above file

#Get-Content demo

# Get-Content <File Path>
$credentialFileContent = Get-Content "C:\Prasham\Articles\PowerShell\getcontent\credentials.txt"

Write-Host " "

Write-Host "All file content - " $credentialFileContent
Write-Host " "

#Get-Content reads file line by line in text file and creates an array
Write-Host "Content at Line 1" $credentialFileContent[0]
Write-Host "Content at Line 1" $credentialFileContent[1] 

Output of the above script will be

PS C:\> C:\Prasham\Articles\PowerShell\getcontent\getcontent.ps1

All file content -  user name - prasham@knowledgejunction1.onmicrosoft.com password - LIFE IS BEAUTIFUL

Content at Line 1 user name - prasham@knowledgejunction1.onmicrosoft.com

Content at Line 1 password - LIFE IS BEAUTIFUL

fig : PowerShell - Get-Content - Reading content of text file
fig : PowerShell – Get-Content – Reading content of text file

Thanks for reading !!! HAVE A FANTASTIC TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

2 Responses

  1. May 22, 2023

    […] Small Tips and Tricks – PowerShell – how to read content from text file – https://knowledge-junction.in/2023/02/10/small-tips-and-tricks-powershell-how-to-read-content-from-t&#8230; […]

  2. October 7, 2023

    […] We have detailed article on how to read text file – Small Tips and Tricks – PowerShell – how to read content from text file – https://knowledge-junction.in/2023/02/10/small-tips-and-tricks-powershell-how-to-read-content-from-t&#8230; […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading