How to provide user name and password when connecting to a network share
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a network share requires credentials, you usually authenticate as part of the SMB connection itself rather than typing a username and password after the folder opens. On Windows, the most direct way is to connect with net use or PowerShell and supply the account that should access the share.
The Common Windows SMB Case
For a share such as \\fileserver\reports, you can connect with a specific user like this:
The * tells Windows to prompt for the password securely instead of putting it on the command line. That is usually the best choice because plain-text passwords can end up in shell history or process listings.
If you want to map the share to a drive letter:
After that, the share is available through drive Z:.
Use PowerShell for a More Scriptable Flow
PowerShell gives you a cleaner way to prompt for credentials and optionally create a persistent drive mapping.
Get-Credential opens a secure prompt, which is safer than embedding a password in a script. -Persist makes the mapping behave like a normal mapped drive that survives outside the current PowerShell session.
If you only need temporary access inside a script, omit -Persist.
Use Credential Manager When Reconnecting Often
If the same share is used regularly, storing the credential in Windows Credential Manager is often more convenient. Then Windows can reuse the credential automatically when the share is accessed again.
That is a better long-term workflow than repeatedly typing credentials or hard-coding them in scripts.
A Very Common Error: Multiple Credentials to the Same Server
One of the most confusing Windows networking issues is error 1219, which happens when you already have a connection to the same server under different credentials. For example, you might already be connected to \\fileserver\public as one user and then try to open \\fileserver\reports as another.
In that case, disconnect the existing session first:
Use the most specific delete command you can. The wildcard form removes all active mapped connections and is more disruptive.
Domain, Local, and Azure-Style User Names
The username format matters. Common patterns include:
- '
DOMAIN\username' - '
SERVERNAME\localuser'
Which one works depends on how the file server authenticates users. If a plain username fails, try the fully qualified domain-style name expected by the environment.
Avoid Plain-Text Passwords in Commands
You can technically pass a password directly to net use, but it is a bad habit:
That exposes the password to shell history and sometimes to monitoring tools. Prefer *, Get-Credential, or a secure secret-management solution.
Programmatic Access Follows the Same Rule
If your application needs to access a protected share, the same credential rules still apply. On Windows, the program typically establishes an authenticated SMB session first, often through system APIs or by running under an account that already has permission.
The key idea is that authentication belongs to the network connection, not to individual file reads after the fact.
Common Pitfalls
- Embedding passwords directly in scripts or command lines.
- Forgetting that an existing SMB session to the same server may block a new connection with different credentials.
- Using the wrong account format, such as omitting the domain when the server expects one.
- Confusing mapped-drive state with credential state. Deleting the drive letter may still leave other connections active.
- Assuming
.txtor application-level login prompts can solve a missing network credential. SMB authentication happens before file access.
Summary
- On Windows, use
net useor PowerShell to connect to a network share with credentials. - Prefer secure prompts such as
*orGet-Credentialinstead of plain-text passwords. - Be aware of error 1219 when connecting to the same server with different accounts.
- Use the correct username format for the environment, such as
DOMAIN\user. - For repeated access, Credential Manager is safer and more convenient than hard-coded secrets.

