1

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
Deltahost
  • 107
  • 3
  • 11

2 Answers2

7

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
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thanks a lot, that's what I was looking for! I've allways tought i need to use a pipe instad of a ">" :-) – Deltahost May 30 '13 at 14:49
  • Can you please explain what 2>&1>null is doing? How does redirecting a number handle errors? – Richard Jan 24 '14 at 07:56
  • Sorry for the late reply. These numbers represent console streams, 2 is the error stream. You can find more information in the about_Redirection help topic – Shay Levy Apr 25 '14 at 11:48
1

The line above works when I run it(piping to out-null). if it still doesn't work for you, try the following:

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

or storing in $null like this:

$null = net use $extractSource $networkCred.Password /USER:$usercred 
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • Unfortunately both options do not work for me... It hides only a success message from the "net use" command but not Error warnings. Per example when i run the command twice I get still a Error like this even I pipe it to out-null: net.exe : System error 1219 has occurred. At line:133 char:16 + $null = net <<<< use $extractSource $networkCred.Password /USER:$usercred + CategoryInfo : NotSpecified: (System error 1219 has occurred.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Multiple connections to a server or shared resource... – Deltahost May 30 '13 at 07:14
  • This is something you should have specified in the question. To handle error output, see Shay's answer. – Frode F. May 30 '13 at 12:16
  • Sorry for the uncertain question... Thank you anyway! – Deltahost May 30 '13 at 14:51