How to disable PowerShell Get-Content output

I have a PS script:

script.ps1

[System.Xml.XmlDocument] $Config; function Get-ScriptDirectory { Split-Path $script:MyInvocation.MyCommand.Path } function Load-Config { $configPath = Join-Path (Get-ScriptDirectory) config.xml $global:Config = [xml](gc $configPath) } Load-Config 

config.xml

 <Configuration> </Configuration> 

Later in the script, I work with the $ Config variable. When I run this script, it writes the output to the console containing the xml root element. Something like:

 Configuration -------------- 

Is there a way to suppress this conclusion?

Thanks.

+7
source share
4 answers

The output is probably not caused by an assignment operation (voidable statement), but on this line:

[System.Xml.XmlDocument] $Config;

In PowerShell, as a rule, all statements return a value (except voidable statements). I think the first time you run the script no output, it will be written to the console. However, on subsequent launches, $Config will still contain the value of the previous run, and its value will be written to the screen.

  • for the Out-Null cmdlet: [System.Xml.XmlDocument] $Config | Out-Null [System.Xml.XmlDocument] $Config | Out-Null
  • void casting: [void][System.Xml.XmlDocument]$Config
  • assignment $ null: $null = $Config
  • or just don't declare the $Config variable

- ways to suppress this behavior.

+6
source

If you do not want to output the command output to the console, you can refuse it by forwarding it or redirecting it to From-Null . For example, both will work:

 $Config | Out-Null $Config > Out-Null 

If you are familiar with Unix-like operating systems, Out-Null is conceptually equivalent to /dev/null .

+4
source

Somewhere you are dumping a variable from your script. Since it drops out of the pipe, it goes to the Out-Host, and this will give the result that you see.

The actual solution is to make sure that you are not returning anything from your script. Since I cannot see your code, I cannot indicate where, but somewhere there is a pipeline or operator that leaks the object to output. Are you sure you are using the appointment anywhere you need?

0
source

Several parameters:

 # Pipe to the Out-Null cmdlet $Config | Out-Null # Cast to void [void]$Config # assign to $null $null = $Config # redirect to $null $Config > $null 
0
source

All Articles