Question

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
Était-ce utile?

La solution

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

Autres conseils

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 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top