Create a new local user and map a network share for the new user

Hey everyone,

I am fairly new to Powershell and I am trying to mount network shares for a newly created local user.

We have no AD, so I am logged in an local admin user.

From there I do

$Password = read-host AsSecurestring

New-LocalUser -Name NewUser -password $Password -PasswordNeverExpires

Add-LocalGroupMember -Group Users -Member NewUser


Now, I want to mount a network share for the “NewUser” (without switching to the NewUser)

Solution that worked, but probalby won’t run from a script in the feature.

"Start-Process -filepath "powershell.exe" -Credential NewUser"

It opens a new powershell window and I can use the

New-PSDrive

cmdlet to map a network share for the “New User”. But first, the new Powershell window is unresponsive as long as I haven’t closed my old Powershell window. This feels very awkward.

Other things I tried.

Enabled-PSremoting
winrm quickconfig
and set the trustedhost -value to *

So I can run the New-PSDrive cmdlet via Invoke-Command with the credentials of the “NewUser” on my localmachine.

Invoke-Command -ComputerName localhost -ScriptBlock{New-PSDrive -Name T -Root \\Server\Share -Persist -Scope Global} -Credential NewUser

This will give me an Access denied error.

Is there even an elegant way to solve my issue?

Best regards,

Tobias

May be a silly question, but why are you trying to connect to a share as a different user than you are logged on as? Is the share a windows share or no?

We don’t have an active directory and we often have freelancer and employees who switch PCs. We always have to create their users manually. My idea was to create a script, which runs from local admin user and basicly does the work. So they can login, have their shares mounted, and the only thing that is left is that they have to sign in into their mail accounts.

I cannot test at the moment but I could imagine that you could use the task scheduler to help you out until you get a proper domain. :wink:
Something like this could be a start:

$Action = New-ScheduledTaskAction -Execute ‘CMD’ -Argument ‘/C NET USE T: \Server\Share’
$Trigger = New-ScheduledTaskTrigger -AtLogOn -User ‘NewUser’
$Settings = New-ScheduledTaskSettingsSet -NetworkName ‘NetworkName’ -RunOnlyIfNetworkAvailable
Register-ScheduledTask -TaskPath ‘\YourCompany’ -TaskName ‘MapShareNewUser’ -Action $Action -Trigger $Trigger -Settings $Settings

Of course you should fine tune the necessary settings.
BTW: Sometimes it’s easier to ask a technical question in your native language - there is a German Microsoft Powershell Forum as well … just in case. :wink:

As soon as I can, I’m going to test it myself!

And thank you for showing me another resource I can use in the future!