Remove-FileShare
Windows: Removes an existing SMB file share
#Requires -Version 5.1
#Requires -Modules SmbShare
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Name,
[string]$ComputerName = $env:COMPUTERNAME,
[switch]$Force,
[pscredential]$Credential
)
Process
{
try
{
$session = $null
$removeParams = @{
'Name' = $Name
'Force' = $Force
'Confirm' = $false
'ErrorAction' = 'Stop'
}
if ($ComputerName -ne $env:COMPUTERNAME)
{
$sessionParams = @{
'ComputerName' = $ComputerName
}
if ($null -ne $Credential)
{
$sessionParams.Add('Credential', $Credential)
}
$session = New-CimSession @sessionParams
$removeParams.Add('CimSession', $session)
}
Write-Verbose "Attempting to remove share '$Name' from '$ComputerName'..."
Remove-SmbShare @removeParams
$result = [PSCustomObject]@{
ShareName = $Name
ComputerName = $ComputerName
Action = "Removed"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
Write-Output $result
}
catch
{
throw
}
finally
{
if ($null -ne $session)
{
Remove-CimSession $session
}
}
}Specifies the name of the share to remove.
Specifies the name of the target computer. Defaults to the local computer.
Off
Indicates that the share should be removed even if there are active connections or open files.
Specifies a PSCredential object for remote connection.