Powershell with FileSystem Read File

Using the Get-Content Cmdlet, we can read the content inside the file

Example


Get-Content c:\powershell-scripts\test.txt

Copy and Try it

Let create some content for file c:\powershell-scripts\test.txt

Example

ServerName:Server01
IP: 10.232.22.01
Location:NY
OS: Win2k8

ServerName:Server02
IP: 10.232.22.02
Location:NY
OS: Win2k12

ServerName:Server03
IP: 10.232.22.03
Location:NY
OS: Win2k3

Copy and Try it


Example


$content = Get-Content c:\powershell-scripts\test.txt | Out-String
$content -split '(?:\r\n){2,}' | Foreach-Object{
    $powershellobject = New-Object PSObject #create the new powershell object
    $_ -split '(?:\r\n)' | Where-Object {$_} | ForEach-Object{  #seperate the content based on new line charators
    $var = $_ -split ':' #the split ":"
    $powershellobject | Add-Member -MemberType NoteProperty -Name $var[0].Trim() -Value $var[1].Trim() # format display the content
}
$powershellobject       #display the content
} 

Copy and Try it