Creating shares via command line/Scripting

Sometimes in IT, we need to be resourceful in order to provide solutions, even though they can be very unorthodox. What happened to me a few days ago was to script the creation of a share. The share in question was located on a removable drive used for backups on a windows server. So before the backup, this script will need to delete any shares of the same name, then create a share called "backup" directly on the root of the removable/USB drive.

To do this, we can use the NET command in a batch file:

NET SHARE sharename:f:\ /grant:everyone,FULL

This will create a share named "sharename" and its location is the root of the F drive, then we will grant everyone the permissions to access the share. THese are share permissions, and they do not modify the NTFS permissions in place, that will be discussed in a future blog post.

NET SHARE sharename /delete /Y

This command deletes the share. the /Y parameter will answer yes if there are still users connected to the share.

So the script in the end will run:

NET SHARE sharename /delete /Y

NET SHARE sharename:f:\ /grant:everyone,FULL