PowerShell keep text formatting when reading in a file

前端 未结 3 1670
花落未央
花落未央 2021-01-11 10:31

I believe this is a simple question, but I can\'t wrap my head around it. I want to do diagnostic commands in command shell on Windows. Like this:

   $cmd =          


        
3条回答
  •  春和景丽
    2021-01-11 10:43

    This happens because of your casting. Get-Content returns an object array with a string object per line in the textfile. When you cast it to [string], it joins the objects in the array. The problem is that you don't specify what to join the objects with (e.g. linebreak (backtick)n).

    ipconfig >> test.txt
    
    #Get array of strings. One per line in textfile
    $message = Get-Content test.txt
    
    #Get one string-object with linebreaks
    $message = (Get-Content test.txt) -join "`n"
    

提交回复
热议问题