Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress PowerShell Get-Content output

Tags:

powershell

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'm working with $Config variable. When I run this script it writes the output to the console, which contains the root element of the xml. Something like:

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

Is there exists any way how to suppress this output?

Thanks.

like image 426
zosim Avatar asked Jan 24 '12 09:01

zosim


People also ask

How do I suppress output in PowerShell?

To silence direct console output and discard all output, you can temporarily disable the internal PowerShell command Out-Default . This command is used privately to write directly to the console. Read more about Out-Default if you like.

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

How do I pause PowerShell?

The most commonly used pause command is by far, Start-Sleep . This command takes two simple inputs, which are -Seconds and -Milliseconds . Seconds can be a System. Double number value while milliseconds takes only System.


1 Answers

If you don't want the output of a command to be printed out to the console, you can discard it by piping it or redirecting it to Out-Null. For example both will work:

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

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

like image 110
Enrico Campidoglio Avatar answered Oct 13 '22 13:10

Enrico Campidoglio