{"id":70703,"date":"2005-01-07T13:47:00","date_gmt":"2005-01-07T13:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/07\/how-can-i-share-a-folder-on-a-remote-computer\/"},"modified":"2005-01-07T13:47:00","modified_gmt":"2005-01-07T13:47:00","slug":"how-can-i-share-a-folder-on-a-remote-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-share-a-folder-on-a-remote-computer\/","title":{"rendered":"How Can I Share a Folder on a Remote Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I share a folder on a remote computer?<BR><BR>&#8212; RS<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RS. One of the great things about WMI is the fact that &#8211; with one or two fairly obscure exceptions &#8211; anything you can do on the local computer you can also do on a remote computer. This is the big advantage scripting has over command line tools; a number of command line tools (including tools like net share) work only on the local computer. If you want to do something remotely, a WMI script is often the only way to go.<\/P>\n<P>So how do you share a folder on a remote computer? Here\u2019s one way:<\/P><PRE class=\"codeSample\">Const FILE_SHARE = 0\nConst MAXIMUM_CONNECTIONS = 25<\/p>\n<p>strComputer = &#8220;atl-ws-01&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objNewShare = objWMIService.Get(&#8220;Win32_Share&#8221;)<\/p>\n<p>errReturn = objNewShare.Create _\n    (&#8220;C:\\Public&#8221;, &#8220;PublicShare&#8221;, FILE_SHARE, _\n        MAXIMUM_CONNECTIONS, &#8220;Public share for Fabrikam employees.&#8221;)\n<\/PRE>\n<P>The preceding script shares out the folder C:\\Public on a computer name atl-ws-01. The script starts by setting a pair of constants. First we assign the value 0 to the constant FILE_SHARE (which indicates the type of shared resource we are creating); if we set the value of this constant to 2147483648 we could create an Administrative share rather than a standard file share. We also assign the value 25 to the constant MAXIMUM_CONNECTIONS, which sets the maximum number of simultaneous connections. If we wanted to allow an unlimited number of simultaneous connections we wouldn\u2019t use this constant at all, and then leave this parameter blank when we create the share.<\/P>\n<P>After connecting to the WMI service on the remote computer we then connect to the Win32_Share class. At that point, all we have to do is call the Create method, passing the following five parameters:<\/P>\n<TABLE class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Parameter<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">\u201cC:\\Public\u201d<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The local path to the folder being shared.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">\u201cPublicShare\u201d<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Share name for the shared folder.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">FILE_SHARE<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The type of share being created.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">MAXIMUM_CONNECTIONS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The maximum number of simultaneous users that can connect to the share.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">\u201cPublic share for Fabrikam employees.\u201d<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Optional description of the shared folder.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That\u2019s about all there is to it; run the script, and the folder C:\\Public will be shared out as PublicShare. Note that the folder C:\\Public must already exist on the computer atl-ws-01; the Win32_Share Create method won\u2019t create the folder for you. If you aren\u2019t sure if the folder C:\\Public exists, here\u2019s a quick way to check that:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Public'&#8221;)\nWscript.Echo colFolders.Count\n<\/PRE>\n<P>This script echoes back the number of folders named C:\\Public (note that in the query itself this must be specified as <B>C:\\\\Public<\/B>) found on the computer atl-ws-01. If the number of folders equals 0, then C:\\Public doesn\u2019t exist. If the number of folders equals 1, then it <I>does<\/I> exist.<\/P>\n<P>By the way, we know a lot of you are going to ask about setting permissions on a shared folder. This can actually be done in a script, but the process is a bit complicated and requires more detailed discussion than we can provide in this column. But we hope to post a considerable amount of material explaining how to work with security descriptors very soon.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I share a folder on a remote computer?&#8212; RS Hey, RS. One of the great things about WMI is the fact that &#8211; with one or two fairly obscure exceptions &#8211; anything you can do on the local computer you can also do on a remote computer. This is the [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,185,12,5],"class_list":["post-70703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I share a folder on a remote computer?&#8212; RS Hey, RS. One of the great things about WMI is the fact that &#8211; with one or two fairly obscure exceptions &#8211; anything you can do on the local computer you can also do on a remote computer. This is the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}