How to hide output from net use in Powershell

Deltahost picture Deltahost · May 29, 2013 · Viewed 8.7k times · Source

I'm using this Powershell script to connect a network path:

$usrLogin = [Environment]::UserName + "@mydomain.net"
$cred = Get-Credential $usrLogin
$networkCred = $cred.GetNetworkCredential()
$usercred = $networkCred.UserName + '@' + $networkCred.Domain
$extractSource = "\\srv01\d$\"
net use $extractSource $networkCred.Password /USER:$usercred

I decide to use "net use", because I'll open later in the Script a FileDialog which opens directly the Network Path.

$openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog1.initialDirectory = "\\srv01\d$\"

Now my question:

How can I hide the output from the "net use" command? I tried this but it didn't work:

net use $extractSource $networkCred.Password /USER:$usercred | out-null

Answer

Shay Levy picture Shay Levy · May 30, 2013

You can redirect the error stream to null:

net use $extractSource $networkCred.Password /USER:$usercred 2>null

Or use 2>&1 to redirect the error stream to the standard output stream and both to null:

net use $extractSource $networkCred.Password /USER:$usercred 2>&1>null