Home > Software engineering >  Powershell If statements write to txt file
Powershell If statements write to txt file

Time:04-08

Ok, I have a script that creates a gui and then runs several "if statements" in order to output lines of text. It will correctly write to the console, but cannot figure out how to get it to a csv file.
Note : Some of the code is not shown due to the length of the script. Notes are included where appropriate.

function Build_GV
{
if ($GVC.checked -eq $true) #GVC is defined in the gui code
{
write-host "GV is present.  Version is $gv_ver" #$gv_ver is in the gui portion of the code
}
if($GPC.checked -eq $true) #GPC defined in gui code
{
write-host "GP is present.  Version is $gv_ver"
}

If the checkboxes are checked, the output on the console is: GV is present. Version is 33.2 GP is present. Version is 33.2

I would like those two lines to be written into a csv file like this: GV is present. Version is 33.2, GP is present. Version is 33.2,

CodePudding user response:

Figured it out.

Added $locations = $( before the first "if" statement. Changed write-host to write-output Then closed the parens at the very end. then before the end of the function, added $locations | out-file -filepath "C:\Testpath\text.txt"

Works like a champ now.

  • Related