Main example
List instances
# All instances of Win32_Share, with the main properties
Get-CimInstance -ClassName Win32_Share |
Select-Object -Property Name, Path, Description, TypeExamples
Filter with WQL
# WQL filter evaluated by the provider: Type = 0 (Disk Drive)
Get-CimInstance -ClassName Win32_Share -Filter 'Type = 0' |
Select-Object -Property Name, Path, Description, TypeThe filter is evaluated by the WMI provider: less data is transferred than with Where-Object.
Decode numeric values
# Lookup table for Type (values documented by Microsoft)
$typeName = @{
0 = 'Disk Drive'
1 = 'Print Queue'
2 = 'Device'
3 = 'IPC'
2147483648 = 'Disk Drive Admin'
2147483649 = 'Print Queue Admin'
2147483650 = 'Device Admin'
2147483651 = 'IPC Admin'
}
Get-CimInstance -ClassName Win32_Share |
Select-Object -Property @(
'Name'
'Path'
'Description'
@{ Name = 'Type'; Expression = { $typeName[[int]$_.Type] } }
)Query a remote computer (CIM session)
review before running# Name of the remote computer
$computerName = 'SERVER01'
# The CIM session (WSMan by default) can be reused for several queries
$cimSession = New-CimSession -ComputerName $computerName
try {
Get-CimInstance -CimSession $cimSession -ClassName Win32_Share |
Select-Object -Property Name, Path, Description, Type
} finally {
Remove-CimSession -CimSession $cimSession
}WSMan (WinRM) is used by default. For a host without WinRM, create the session with -SessionOption (New-CimSessionOption -Protocol Dcom). Add -Credential to use another account.
Show the class structure
# Class structure: properties, types and flags (Key, ReadOnly…)
$cimClass = Get-CimClass -ClassName Win32_Share
$cimClass.CimClassProperties |
Select-Object -Property Name, CimType, Flags
# Methods of the class
$cimClass.CimClassMethods |
Select-Object -Property Name, ReturnTypeProperties
| Name | Type | Access | Description | |||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| AccessMask# | uint32 | read | This property is obsolete and is no longer used. Use the Win32_Share.GetAccessMask method instead. The value of the AccessMask property is set to null by WMI. For more information about setting access when a share is created, see the Create method. Code | |||||||||||||||||||||||||||||||||||||||
| AllowMaximum# | boolean | read | Number of concurrent users for this resource has been limited. If True, the value in the MaximumAllowed property is ignored. Code | |||||||||||||||||||||||||||||||||||||||
| Caption# | string | read | A short textual description of the object. Code | |||||||||||||||||||||||||||||||||||||||
| Description# | string | read | A textual description of the object. Code | |||||||||||||||||||||||||||||||||||||||
| InstallDate# | datetime | read | Indicates when the object was installed. Lack of a value does not indicate that the object is not installed. Code | |||||||||||||||||||||||||||||||||||||||
| MaximumAllowed# | uint32 | read | Limit on the maximum number of users allowed to use this resource concurrently. The value is only valid if the AllowMaximum property is set to FALSE. Code | |||||||||||||||||||||||||||||||||||||||
| Name# key | string | read | Alias given to a path set up as a share on a computer system running Windows. Windows 2008 example: "\SERVER01\public" - Windows Server 2008 requires that you place the UNC in the name. Code | |||||||||||||||||||||||||||||||||||||||
| Path# | string | read | Local path of the Windows share. Example: "C:\Program Files" Code | |||||||||||||||||||||||||||||||||||||||
| Status# | string | read | String that indicates the current status of the object. Operational and non-operational status can be defined. Operational status can include "OK", "Degraded", and "Pred Fail". "Pred Fail" indicates that an element is functioning properly, but is predicting a failure (for example, a SMART-enabled hard disk drive). Non-operational status can include "Error", "Starting", "Stopping", and "Service". "Service" can apply during disk mirror-resilvering, reloading a user permissions list, or other administrative work. Not all such work is online, but the managed element is neither "OK" nor in one of the other states. Values include the following: Values (12)
Code | |||||||||||||||||||||||||||||||||||||||
| Type# | uint32 | read | Type of resource being shared. Types include: disk drives, print queues, interprocess communications (IPC), and general devices. Values (8)
Code |
Methods
Create static#
Class method that initiates sharing for a server resource.
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
| Parameter | Type | Direction | Description |
|---|---|---|---|
| Path | string | in | Local path of the Windows share. Example, "C:\Program Files". |
| Name | string | in | Passes the alias to a path set up as a share on a computer system running Windows. Example, "public". |
| Type | uint32 | in | 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. |
| MaximumAllowed | uint32 | in | Limit on the maximum number of users allowed to concurrently use this resource. Example: 10. This parameter is optional. |
| Description | string | in | Optional comment to describe the resource being shared. This parameter is optional. |
| Password | string | in | 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 | Win32_SecurityDescriptor | in | Security descriptor for user level permissions. A security descriptor contains information about the permissions, owner, and access capabilities of the resource. If this parameter is not supplied or is NULL, then Everyone has read access to the share. For more information, see Win32_SecurityDescriptor and Changing Access Security on Securable Objects. |
Return values
| Value | Label | Description |
|---|---|---|
| 0 | Success | |
| 2 | Access denied | |
| 8 | Unknown failure | |
| 9 | Invalid name | |
| 10 | Invalid level | |
| 21 | Invalid parameter | |
| 22 | Duplicate share | |
| 23 | Redirected path | |
| 24 | Unknown device or directory | |
| 25 | Net name not found | |
| 26..4294967295 | Other |
Call the Create method
review before running# Warning: this method can change the system. -WhatIf shows the operation without running it.
$methodParameters = @{
ClassName = 'Win32_Share'
MethodName = 'Create'
Arguments = @{
Path = 'C:\Share'
Name = 'ExampleShare'
Type = 0
MaximumAllowed = 0
Description = 'Example'
Password = 'Example'
Access = $null
}
WhatIf = $true # Remove this line to actually run the method
}
$result = Invoke-CimMethod @methodParameters
# With -WhatIf nothing runs and no result is returned
if ($result) {
$returnValueName = @{
0 = 'Success'
2 = 'Access denied'
8 = 'Unknown failure'
9 = 'Invalid name'
10 = 'Invalid level'
21 = 'Invalid parameter'
22 = 'Duplicate share'
23 = 'Redirected path'
24 = 'Unknown device or directory'
25 = 'Net name not found'
}
$returnValueName[[int]$result.ReturnValue]
}This method can change the system. The -WhatIf parameter shows the operation without running it: remove it only after checking.
Delete #
Class method that deletes a share name from a server's list of shared resources, disconnecting connections to the shared resource.
uint32 Delete();Return values
| Value | Label | Description |
|---|---|---|
| 0 | Success | |
| 2 | Access denied | |
| 8 | Unknown failure | |
| 9 | Invalid name | |
| 10 | Invalid level | |
| 21 | Invalid parameter | |
| 22 | Duplicate share | |
| 23 | Redirected path | |
| 24 | Unknown device or directory | |
| 25 | Net name not found | |
| 26..4294967295 | Other |
Call the Delete method
review before running# Warning: this method can change the system. -WhatIf shows the operation without running it.
# Target instance
$instance = Get-CimInstance -ClassName Win32_Share -Filter "Name = 'ExampleShare'"
$methodParameters = @{
InputObject = $instance
MethodName = 'Delete'
WhatIf = $true # Remove this line to actually run the method
}
$result = Invoke-CimMethod @methodParameters
# With -WhatIf nothing runs and no result is returned
if ($result) {
$returnValueName = @{
0 = 'Success'
2 = 'Access denied'
8 = 'Unknown failure'
9 = 'Invalid name'
10 = 'Invalid level'
21 = 'Invalid parameter'
22 = 'Duplicate share'
23 = 'Redirected path'
24 = 'Unknown device or directory'
25 = 'Net name not found'
}
$returnValueName[[int]$result.ReturnValue]
}This method can change the system. The -WhatIf parameter shows the operation without running it: remove it only after checking.
GetAccessMask #
Returns the access rights to the share held by the user or group on whose behalf the instance is returned. You should use this method in place of the AccessMask property, which is always NULL.
uint32 GetAccessMask();Return values
| Value | Label | Description |
|---|---|---|
| 1 | FILE_LIST_DIRECTORY | Grants the right to read data from the file. For a directory, this value grants the right to list the contents of the directory. |
| 2 | FILE_ADD_FILE | Grants the right to write data to the file. For a directory, this value grants the right to create a file in the directory. |
| 4 | FILE_ADD_SUBDIRECTORY | Grants the right to append data to the file. For a directory, this value grants the right to create a subdirectory. |
| 8 | FILE_READ_EA | Grants the right to read extended attributes. |
| 16 | FILE_WRITE_EA | Grants the right to write extended attributes. |
| 32 | FILE_TRAVERSE | Grants the right to execute a file. For a directory, the directory can be traversed. |
| 64 | FILE_DELETE_CHILD | Grants the right to delete a directory and all of the files it contains (its children), even if the files are read-only. |
| 128 | FILE_READ_ATTRIBUTES | Grants the right to read file attributes. |
| 256 | FILE_WRITE_ATTRIBUTES | Grants the right to change file attributes. |
| 65536 | DELETE | Grants delete access. |
| 131072 | READ_CONTROL | Grants read access to the security descriptor and owner. |
| 262144 | WRITE_DAC | Grants write access to the discretionary access control list (DACL). |
| 524288 | WRITE_OWNER | Assigns the write owner. |
| 1048576 | SYNCHRONIZE | Synchronizes access and allows a process to wait for an object to enter the signaled state. |
Call the GetAccessMask method
review before running# Warning: this method can change the system. -WhatIf shows the operation without running it.
# Target instance
$instance = Get-CimInstance -ClassName Win32_Share -Filter "Name = 'ExampleShare'"
$methodParameters = @{
InputObject = $instance
MethodName = 'GetAccessMask'
WhatIf = $true # Remove this line to actually run the method
}
$result = Invoke-CimMethod @methodParameters
# With -WhatIf nothing runs and no result is returned
if ($result) {
$returnValueName = @{
1 = 'FILE_LIST_DIRECTORY'
2 = 'FILE_ADD_FILE'
4 = 'FILE_ADD_SUBDIRECTORY'
8 = 'FILE_READ_EA'
16 = 'FILE_WRITE_EA'
32 = 'FILE_TRAVERSE'
64 = 'FILE_DELETE_CHILD'
128 = 'FILE_READ_ATTRIBUTES'
256 = 'FILE_WRITE_ATTRIBUTES'
65536 = 'DELETE'
131072 = 'READ_CONTROL'
262144 = 'WRITE_DAC'
524288 = 'WRITE_OWNER'
1048576 = 'SYNCHRONIZE'
}
$returnValueName[[int]$result.ReturnValue]
}This method can change the system. The -WhatIf parameter shows the operation without running it: remove it only after checking.
Remarks
The Win32_Share class is derived from CIM_LogicalElement.
The Create method in this class is a static method. The Delete, GetAccessMask and SetShareInfo methods are all instance methods.
Depending on your security permissions, you may not be able to retrieve all of the properties of this class. For example, AllowMaximum, MaximumAllowed, Path, and Type properties may return null. Generally speaking, Power Users and Administrators will be able to retrieve all property values.
Inheritance
Superclass chain
- CIM_ManagedSystemElement
- CIM_LogicalElement
- Win32_Share
Known subclasses
Requirements
- Namespace
- root/cimv2
- Minimum client
- Windows Vista
- Minimum server
- Windows Server 2008
- Provider
- CIMWin32
- MOF file
- CIMWin32.mof
- DLL
- CIMWin32.dll
Class qualifiers
Dynamic Provider("CIMWin32") UUID("{8502C4D6-5FBB-11D2-AAC1-006008C78BC7}") SupportsCreate CreateBy("Create") SupportsDelete DeleteBy("DeleteInstance") AMENDMENT
Microsoft source
Descriptions, properties and methods come from the Microsoft documentation (MicrosoftDocs/win32 repository), published under the CC BY 4.0 licence. Formatting and translations are specific to this site; the PowerShell examples are written for it.