Using Powershell how can I create a share and set access permissions.

For example as follows

  • Create share called "public" that maps to the "path c:\shares\foo"
  • Allow DOMAIN1\Users to have read-only access to the share (this does not mean setting acls on the files, rather on the share)
link|improve this question

50% accept rate
feedback

3 Answers

This should do the trick:

net share "Public=c:\shares\foo" "/GRANT:Users,READ"

Of course, you'll need to launch PowerShell with administrative rights, depending where/how you're doing this.

link|improve this answer
feedback

Use the Win32_Share Create method. Example:

(Get-WmiObject -List -ComputerName . | Where-Object -FilterScript 
{$_.Name -eq "Win32_Share"}).InvokeMethod("Create",
   ("C:\FolderToShare","ShareName",0,100,"Share description"))

You can find the documentation of this method here on MSDN.

uint32 Create(
  [in]  string Path,
  [in]  string Name,
  [in]  uint32 Type,
  [in]  uint32 MaximumAllowed,
  [in]  string Description,
  [in]  string Password,
  [in]  Win32_SecurityDescriptor Access
);

Parameters:

  • Path - Local path of the Windows share. For example, "C:\FolderToShare".
  • Name - Passes the alias to a path set up as a share on a Windows system. Example, "ShareName".
  • Type - Passes the type of resource being shared. Types includes disk drives, print queues, interprocess communications (IPC), and general devices. Can be one of the following values.
    • 0 - Disk Drive
    • 1 - Print Queue
    • 2 - Device
    • 3 - IPC
    • 2147483648 - Disk Drive Admin
    • 2147483649 - Print Queue Admin
    • 2147483650 - Device Admin
    • 2147483651 - IPC Admin
  • MaximumAllowed - Limit on the maximum number of users allowed to concurrently use this resource. Example: 100. This parameter is optional.
  • Description - Optional comment to describe the resource being shared. This parameter is optional. Example: "Share description".
  • Password - Password (when the server is running with share-level security) for the shared resource. If the server is running with user-level security, this parameter is ignored. This parameter is optional.
  • Access - Security descriptor for user level permissions. A security descriptor contains information about the permissions, owner, and access capabilities of the resource.

See this page on MSDN for details how to set access permissions: Win32_SecurityDescriptor Class. This article is also a good starting point: WMI Tasks: Files and Folders.

link|improve this answer
feedback

This thread, starting with the second answer, has some script examples to accomplish this.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.