1

I want to output the return of this process. Anyone can help me please. Thank you.

$name = $false 
switch -regex -file .\bios.txt {
    '^Product Name' { $name = $true; continue }
    '^\s' { if ($name) { $_.Trim() }}
    '^\S' { if ($name) { return } Out-File .\PN.txt}

}

I tried that way, but the output file is empty.

1 Answer 1

1

The Out-File .\PN.txt command is only ever reached for (a) lines that start with a non-whitespace character (\S) while (b) $name isn't $true. When it is reached, it creates an empty .\PN.txt file, due to lack of input.

If, perhaps, your intent was to send all output from the switch statement to a file, try the following:

$name = $false 
& { 
  switch -regex -file .\bios.txt {
    '^Product Name' { $name = $true; continue }
    '^\s' { if ($name) { $_.Trim() }}
    '^\S' { if ($name) { return } $_ }

  }
} | Out-File .\PN.txt

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.