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
0

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, that's what I was looking for! I've allways tought i need to use a pipe instad of a ">" :-)
Can you please explain what 2>&1>null is doing? How does redirecting a number handle errors?
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
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 

3 Comments

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...
This is something you should have specified in the question. To handle error output, see Shay's answer.
Sorry for the uncertain question... Thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.