Save content from text file to string variable

Filip 831 Reputation points
2021-05-18T15:49:46.527+00:00

Hello everybody.

I add content from file to my variable but the variable is save as Name:"object" and BaseType:"system.array".
I want convert this variable to normal string.
But when i done it the string is messed up.

See what i mean ("$test" is the right variable and "test1" is what i want to look same as the previos variable.):

97549-a.png

Code example:

$test = "  
This  
 is  
test  
code  
"  
$test1 = cat Path-To-File-Where-Is-The-Same-Text-What-I-Used-Previos  
$test.GetType()  
  
$test1.GetType()  
[string]$test1  
$test  
$test1  
[System.Object]$test1  
$a = [System.Object]$test1  
$a  
$a.GetType()  

I want to print same output as the variable $test

Thanks for help.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Andreas Baumgarten 129.4K Reputation points MVP Volunteer Moderator
    2021-05-18T16:25:14.91+00:00

    Hi @Filip ,

    have you tried Get-Content instead of cat?

    $content = Get-Content -Path $fileName -Raw  
    $content.GetType()  
      
    <#  
    IsPublic IsSerial Name                                     BaseType  
    -------- -------- ----                                     --------  
    True     True     String                                   System.Object  
    #>  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.