We help IT Professionals succeed at work.
Get Started

Powershell "out-file" command write blank file.

6,162 Views
Last Modified: 2014-05-05
I have a powershell script that opens an XML document. Reads each line. Modifies specific ones based on some values it receives. Then writes the lines back changed/unchanged as needed. I think the logic works because I have some "write-host" entries that display what is going on in the code.

But when the script finished the orignal file "c:\tempxyz\test.txt" is completely blank. The script has completely erased everything. I'm not sure where I have gone wrong.

Below is my script and ***samples of the output and the @@@ source test.txt file
-------------------------------------------

cls
$Filename = "c:\tempxyz\test.txt"

write-host -foregroundcolor "yellow" "Are you switching from :"
write-host "A = Training To Prod
write-host "B = Prod To Training

$ProdOrPreview = read-host "Choice A or B? "

(Get-Content $Filename) | Foreach-Object {            
      $TrimmedLine=$_.trim() #(Takes "$_" and removes the leading spaces
      If ($TrimmedLine.StartsWith("<JobName>")) {
            $a = $TrimmedLine -match "<\w+>(.*?)</\w+>"
            $JobName = $Matches[1]
            write-host "JobName is $JobName"
            }
      If ($TrimmedLine.StartsWith("<DrawerName>")) {
            if ($ProdOrPreview -eq 'B') {$Drawername = "Training"}
            if ($ProdOrPreview -eq 'A') {$Drawername = $JobName}
            $_ = "<DrawerName>"+$DrawerName+"</DrawerName>" #formats line to be re-written back into test.txt
            write-host "   Writing to file $_" #lets user know what got written into test.txt
            }
      } | out-file $FileName



--------------------------------------------------

*** Here is what outputs on the screen when I run it :
--------------------------------------------------
Are you switching from :
A = Training To Prod
B = Prod To Training
Choice A or B? : B

JobName is Commercial
   Writing to file <DrawerName>Training</DrawerName>
JobName is Personal
   Writing to file <DrawerName>Training</DrawerName>

-------------------------------------------------------

@@@ Below is sample test.txt file before it is edited
-------------------------------------------------------
    <MappingJobConfiguration>
                  <JobName>Commercial</JobName>
      <LocationName />
                 <DrawerName>Commercial</DrawerName>
    </MappingJobConfiguration>
    <MappingJobConfiguration>
                  <JobName>Personal</JobName>
      <JobDescription>Personal</JobDescription>
      <LocationName />
                   <DrawerName>Personal</DrawerName>
Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 15 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE