[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("ISO-8859-1") # Lancer la transcription Start-Transcript -Path c:\windows\temp\printers.log #Read printers.csv as input $Printers = Import-Csv .\printers.csv # Afficher la liste des imprimantes disponibles Write-Host "Imprimantes disponibles :" for ($i = 0; $i -lt $Printers.Count; $i++) { Write-Host "$($i + 1). $($Printers[$i].Name)" } # Laisser l'utilisateur choisir les imprimantes à installer (séparées par des virgules) $choices = Read-Host "Entrez le numero des imprimantes a installer (separes par des virgules)" $selectedPrinters = @() $selectedChoices = $choices -split "," foreach ($choice in $selectedChoices) { $choice = $choice.Trim() if ($choice -match '^\d+$' -and [int]$choice -ge 1 -and [int]$choice -le $Printers.Count) { $selectedPrinters += $Printers[[int]$choice - 1] } } #Add all printer drivers by scanning for the .inf files and installing them using pnputil.exe $infs = Get-ChildItem -Path . -Filter "*.inf" -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Fullname $totalnumberofinfs = $infs.Count $currentnumber = 1 Write-Host ("[Install printer driver(s)]`n") -ForegroundColor Green Foreach ($inf in $infs) { Write-Host ("[{0}/{1}] Adding inf file {2}" -f $currentnumber, $totalnumberofinfs, $inf) -ForegroundColor Green try { c:\windows\sysnative\Pnputil.exe /a $inf | Out-Null } catch { try { c:\windows\system32\Pnputil.exe /a $inf | Out-Null } catch { C:\Windows\SysWOW64\pnputil.exe /a $inf | Out-Null } } $currentnumber++ } #Add selected printer drivers to Windows using the CSV list for the correct names $selectedDriverNames = $selectedPrinters | Select-Object -ExpandProperty DriverName | Select-Object -Unique $totalnumberofdrivers = $selectedDriverNames.Count $currentnumber = 1 Write-Host ("`n[Add selected printerdriver(s) to Windows]") -ForegroundColor Green foreach ($driverName in $selectedDriverNames) { Write-Host ("[{0}/{1}] Adding printerdriver {2}" -f $currentnumber, $totalnumberofdrivers, $driverName) -ForegroundColor Green Add-PrinterDriver -Name $driverName $currentnumber++ } #Loop through selected printers and add the Printer port, the printer and associate it with the port and set the color options to 0 which is black and white (1 = automatic and 2 = color) $totalnumberofprinters = $selectedPrinters.Count $currentnumber = 1 Write-Host ("`n[Add selected printer(s) to Windows]") -ForegroundColor Green foreach ($printer in $selectedPrinters) { Write-Host ("[{0}/{1}] Adding printer {2}" -f $currentnumber, $totalnumberofprinters, $printer.Name) -ForegroundColor Green #Set options for adding printers and their ports $PrinterAddOptions = @{ ComputerName = $env:COMPUTERNAME Comment = $printer.Comment DriverName = $printer.DriverName Location = $printer.Location Name = $printer.Name PortName = $printer.Name } $PrinterConfigOptions = @{ Color = 0 DuplexingMode = 'TwoSidedLongEdge' PrinterName = $printer.Name } $PrinterPortOptions = @{ ComputerName = $env:COMPUTERNAME Name = $printer.Name PrinterHostAddress = $printer.PortName PortNumber = '9100' } #Check if the printer already exists and remove it $existingPrinter = Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Name = '$($printer.Name)'" if ($existingPrinter -ne $null) { $existingPrinter.Delete() } #Check if the printer port already exists and remove it $existingPort = Get-WmiObject -Query "SELECT * FROM Win32_TCPIPPrinterPort WHERE Name = '$($printer.Name)'" if ($existingPort -ne $null) { $existingPort.Delete() } #Add printer port and configure it with the required options Add-PrinterPort @PrinterPortOptions Add-Printer @PrinterAddOptions -ErrorAction SilentlyContinue Set-PrintConfiguration @PrinterConfigOptions # Afficher un message de succès Write-Host ("Imprimante {0} installee avec succes!" -f $printer.Name) -ForegroundColor Green $currentnumber++ } Start-Sleep -Seconds 3 # Arrêter la transcription Stop-Transcript