I want to modify certain network sharing options using a PowerShell script. Specifically, I need to automate the following actions, accessible via:
Windows + R -> control /name Microsoft.NetworkAndSharingCenter /page Advanced
In the "All Networks" section → "Public folder sharing", select:
"Turn on sharing so anyone with network access can read and write files in the Public folders"
In the "File sharing connections" section, select:
"Use 128-bit encryption to help protect file sharing connections (recommended)"
In the "Password protected sharing" section, select:
"Turn off password protected sharing"
I have developed the following PowerShell script. It runs without any errors, but the desired options are not selected. The script has no effect on the selection of the options. Do you have any idea what might be causing the issue?
# 16. Modify sharing settings
if ($config.Configurations.PartageAvance) {
Write-Log "16. Configuring advanced sharing settings..."
try {
# Enable file and printer sharing for all network profiles
$networkProfiles = @("Private", "Public", "Domain")
# Use Get-NetFirewallRule to find sharing rules
$fileAndPrinterSharingRules = Get-NetFirewallRule | Where-Object {
$_.DisplayGroup -like "*File and Printer Sharing*"
}
foreach ($rule in $fileAndPrinterSharingRules) {
Set-NetFirewallRule -Name $rule.Name -Enabled True -Profile $networkProfiles
}
# Alternative method using netsh
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes | Out-Null
# Disable password-protected sharing
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa"
Set-ItemProperty -Path $regPath -Name "LmCompatibilityLevel" -Value 1 -Type DWord
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters"
Set-ItemProperty -Path $regPath -Name "RequireSecuritySignature" -Value 0 -Type DWord
Set-ItemProperty -Path $regPath -Name "EnableSecuritySignature" -Value 0 -Type DWord
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters"
Set-ItemProperty -Path $regPath -Name "RequireSecuritySignature" -Value 0 -Type DWord
Set-ItemProperty -Path $regPath -Name "EnableSecuritySignature" -Value 0 -Type DWord
Set-ItemProperty -Path $regPath -Name "RestrictNullSessAccess" -Value 0 -Type DWord
Write-Log "Sharing settings configured" -ForegroundColor Green
}
catch {
Write-Log "Error configuring sharing settings: $_" -ForegroundColor Red
}
}
else {
Write-Log "Sharing settings configuration disabled" -ForegroundColor Yellow
}