r/PowerShell • u/Ok-Volume-3741 • Nov 25 '25
Problems mapping printers with PowerShell launched from a GPO
Problems mapping printers with PowerShell launched from a GPO
I have the following script that is launched from a GPO at computer startup, and the script is located in a shared folder (I assume with the system user):
cls
$LOG = "\\dominio\SysVol\dominio\scripts\Impresora\Logs\$(hostname).log"
function escribir_log([string]$nivel, [string]$msg) {
write-output "$((Get-Date -Format 'dd/MM/yyyy HH:mm'))`t$($nivel)`t$($msg)" | Tee-Object -FilePath $LOG -Append
}
function main {
escribir_log "INFO" "Ejecutando script Instalar_impresora..."
$impresoraAntigua = (Get-WmiObject -Class Win32_Printer | Where-Object { $_.Name -like "*10.10.10.5*" }).name
$impresoraNueva = "\\10.10.10.10\FollowMe"
$impresoraAntiguaInstalada = (Get-Printer).name -eq $impresoraAntigua
$impresoraNuevaInstalada = (Get-Printer).name -eq $impresoraNueva
if ($impresoraAntiguaInstalada) {
escribir_log "INFO" "Borrando impresora antigua..."
Remove-Printer -Name $impresoraAntigua -ErrorAction SilentlyContinue
}
if(-not $impresoraNuevaInstalada){
try {
escribir_log "INFO" "Instalando impresora..."
rundll32 printui.dll,PrintUIEntry /q /in /n $impresoraNueva
} catch {
escribir_log "ERROR" "Error al Instalar impresora nueva..."
}
}
$impresoraPredeterminadaActual = (Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Default=$true").Name
if($impresoraPredeterminadaActual -ne $impresoraNueva) {
escribir_log "INFO" "Poniendo ${impresoraNueva} como predeterminada..."
sleep 10
rundll32 printui.dll,PrintUIEntry /y /n $impresoraNueva
}
}
mainThe script runs fine, but it's not removing the printer or mapping the new one. If I log into the computer and run it manually, it works without a problem. Does anyone know what's happening? Should I copy the script to a local path on the same computer and run it from there?
2
Upvotes
View all comments
Show parent comment
1
u/faulkkev Nov 26 '25
Well before print nightmare vuln and maybe even now there was a way to deploy printers and install drivers using a gpo, which allowed printer driver install. It allowed non admins to add the printers and drivers. 98% of our company is not admins and it worked. That either with some print nightmare considerations or just trying it by enabling the setting for your efforts should work. Sorry I can’t recall exact setting but if you lookup print nightmare ms I bet had options for remediation which would show you the gpo settings.