PowerShell Script to Read a Text File from an SMB Share

by Simon Sparks · 23 September 2026
[System.Security.SecureString]$strPasswordSecureString = ConvertTo-SecureString -String $strPassword -AsPlainText -Force;

[System.Management.Automation.PSCredential]$objPSCredential = New-Object System.Management.Automation.PSCredential ($strUsername, $strPasswordSecureString);

[System.Management.Automation.PSDriveInfo]$objPSDriveInfo = New-PSDrive -Name "RemoteSMB" -PSProvider FileSystem -Root "\\$strServerName\$strShareName" -Credential $objPSCredential;

[System.String]$strFileContent = Get-Content -Path "$objPSDriveInfo.Name:\$strFolderName\$strFileName.txt";

Remove-PSDrive -Name "RemoteSMB"

Write-Host $strFileContent;

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like