- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- Thomas Lee
- 168字
- 2021-07-02 18:15:58
How to do it...
First, let's look at how you change the spool folder using the .NET Framework:
- Load the System.Printing namespace and classes:
Add-Type -AssemblyName System.Printing
- Define the required permissions—that is, the ability to administrate the server:
$Permissions =
[System.Printing.PrintSystemDesiredAccess]::
AdministrateServer
- Create a PrintServer object with the required permissions:
$Ps = New-Object
-TypeName System.Printing.PrintServer ` -ArgumentList $Permissions
- Update the default spool folder path:
$Newpath = 'C:\Spool' $Ps.DefaultSpoolDirectory = $Newpath
- Commit the change:
$Ps.Commit()
- Restart the Spooler to accept the new folder:
Restart-Service -Name Spooler
- Once the Spooler has restarted, view the results:
New-Object -TypeName System.Printing.PrintServer | Format-Table -Property Name,
DefaultSpoolDirectory
Another way to set the Spooler directory is by directly editing the registry as follows:
- First stop the Spooler service:
Stop-Service -Name Spooler
- Set the spool directory registry setting:
PS C:\foo> $RPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\ +
Print\Printers' $Spooldir = 'C:\SpoolViaRegistry' Set-ItemProperty -Path $RPath ` -Name DefaultSpoolDirectory ` -Value 'C:\SpoolViaRegistry'
- Restart the Spooler:
Start-Service -Name Spooler
- View the results:
New-Object -TypeName System.Printing.PrintServer | Format-Table -Property Name,
DefaultSpoolDirectory