If your script is running on another machine and PowerShell remoting is configured you can also do this with remote scripts. Before using Enter-PSHostProcess open a session with Enter-PSSession -ComputerName and follow the same steps.

If you want more details about this functionality lookup the following commands on Microsofts site.

The implementation of PowerShell runspaces gives us a lot of flexibility and allows for some crazy scenarios. Say I want to run a script and let it break at some point with the Wait-Debugger statement and continue debugging with PowerShell ISE. When I add the Wait-Debugger statement the PowerShell console running the script breaks at the statement in the cmd-line debugger of PowerShell. Thats not what I want, I want PowerShell ISE. You can work-around this by using a helper script which executes your script.

Now change your code to use the password from the connection string to authenticate on the admin database. I assume you have read the connectionstring from the config file and created the MongoUrl or MongoClient somewhere in your code. Add MongoClientSettings after the creation of the MongoUrl and use the new MongoClientSettings to create the MongoClient. Modify the MongoClientSettings to authencicate on the admin database. In code this looks like:

Since resent events an unsecured MongoDB Server in the open is not a good security practice and by default no system account or database user account is created when MongoDB is installed, you need to do it by yourself.
It's not that difficult but another step to do.

I will go over the steps on a Windows installation of MongoDB. I assume you have installed MongoDB with the MSI Installer and run it as a Windows Service.

We are now going to create a MongoDB user which has full access to the MongoDB Server. This means all databases available on this server and databases that will be created in the future.
Hence the database masterkey/system administrator paradigm.

Now start the mongo shell (mongo.exe from the program files folder) and enter the following statements

Now we need to re-start the MongoDB server but with security enforcement enabled by default. On Windows we can do this by editing the registry or changing the config file if used.
I show you how to do this in the registry.

You are now connected to a secure MongoDB server and when running this public users need to authenticate.
To further strengthen security, it's advisable to create a more granular security access to your MongoDB Server/Databases by creating more users with lesser security roles.
See for more details the MongoDB Manuals.

When running your server public it is also a good practise to use a ssl connection to your MongoDB server but this requires another post.