How to connect to an SMB share and write to a file in Powershell without mounting?

Viewed 6660

I've gone through the documentation here. Oddly enough, I can't find anything mentioning how to simply connect to (not mount) an SMB share with PowerShell. I'm looking for the equivalent of this in Python:

with smbclient.open_file(args.share, username=args.smbuser, password=args.smbpass, mode='w', encoding='utf-8-sig', newline='') as file:
  file.write("stuff")
1 Answers

You could use the Windows' net utility

net use \\server /user:domain\username password

Or use PowerShell cmdlet New-SmbMapping from the SmbShare module:

New-SmbMapping -RemotePath '\\server' -Username "domain\username" -Password "password"

After that, you can simply use the UNC path for writing to the file, for example with Out-File

"Your output" | Out-File "\\server\path\myfile.txt"
Related