Create a script executable solution to map a shared folder

In a previous article we demonstrated a number of script packager solutions. In this post we show other ways to provide information to a script executable.

The sample script is used to map a network drive to an existing shared folder. We’re going to use both the “New-PSDrive” cmdlet and the “net use .. /delete” DOS command to make it work.

Sample script

In this script, we’ll be using both the Read-Host and the Get-Credential cmdlets to pass some information to a script executable.

First we will prompt to enter the drive letter to be used to map to a network shared folder.

In this scenario the network shared folder is already provided within the code. The next step is to provide the user credentials for connecting to the network shared folder.

Executing script before creating the executable
Enter User Credentials
Script Result
Verify mapped drive using Get-PSDrive cmdlet

In the logic we check to see if the drive is already in use. If the drive letter is already in use, then it will proceed to remove the existing one attached to the network shared folder connection.

Existing drive will be removed

This solution will work on Windows 7, Windows 10, and Windows Server 2012 (or greater).

Now, lets create a couple of package solutions.

Packaging Options

In the packager, we are going to use the following script engines:

  • SAPIEN PowerShell V5 Host (Command Line)
  • SAPIEN PowerShell V5 Host (Windows Application)

In the packager “Output Settings” section, in the “Icon file” field we can take advantage of the free SAPIEN Icon library and add an icon from the list. The “Folder.ico” icon was selected.

SAPIEN Icon library

This way the icon will be included when building the executable.

Creating a Command Line executable

The packager script engine option “SAPIEN PowerShell V5 Host (Command Line)” gives you the ability to run the executable from the command line, or from File Explorer.

For the command line example, we created the executable “mapNetDriveCLI.exe“. When running it from a shell console it will prompt to enter for both the drive letter and user credentials.

Running the executable

When running the executable again with an already used drive letter, it will proceed to remove the connection to the shared folder.

Removing existing drive

Creating the Windows Application executable

Although the script is not a Windows application, using the packager script engine “SAPIEN PowerShell V5 Host (Windows Application)” will give it the look and feel of a windows application. A pop-up window will prompt to first to enter the drive letter and then the user credentials.

For the Windows Application example, we created the executable “mapNetDrive.exe“. When running it from a shell console or the file browser, a windows pop-up form will prompt to enter for both the drive letter and then the user credentials.

Windows application prompt for drive letter
Windows application prompt for user credential
Successful completion

And again, testing running the script executable will remove the existing drive letter.

Windows application drive letter auto-removal

Another mapping option

In Windows 10, and Windows Server 2016 (or greater), you’ll find the SmbShare module. You could use both the New-SmbMapping and the Remove-SmbMapping instead of the PSDrive cmdlets.

SMBMapSharedFolder.ps1

Summary

As you can see, both the script and the executables created will behave the same. This script has a lot of room for enhancements.

Feel free to experiment and develop a script that will work for you. You can build the package executable using either of our SAPIEN editors: PrimalScript, or PowerShell Studio.

Download the sample script code: SampleMapDrive.zip

Resources

Feedback

As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.

Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at maxt@sapien.com