Powershell Script to remove printers and drivers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am learning Powershell Scripting as I move my work environment to Windows 10. The script below is to be edited by the SysAdmin (for the list of Print Queues and Printer Drivers) to specify what needs to be removed, then outputs what it did. I am looking for whether the Powershell syntax / convention is followed. This script will be distributed to other SysAdmins in my team when completed.



The script currently starts by declaring variables that will be used throughout the script. The variables tell the script which print queues to remove, and what print drivers to remove. It then loops through the arrays, removing the printers requested.



# Remove old printers by name - Written by Luke Barone
#
# VARIABLES TO EDIT
# $PrintersToRemove - (REQUIRED) An array of printers to cycle through and
# remove. Use the exact name of the printer to have it included in the list
# $PrintDriversToRemove - (optional) An array of printer drivers to cycle
# through and remove. You can obtain a list by running the "prndrvr.vbs -l"
# command on a computer with drivers to remove. Alternatively, you can
# leave the array empty, and the script will remove any unused (determined
# by the system) print drivers automatically.
$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

# The variables below should only be changed from the default if the scripts
# are not in this location!
$PrintingAdminScriptsFolder = "C:WindowsSystem32Printing_Admin_Scriptsen-US"
$PrintScriptFiles = @("prnmngr.vbs", "prndrvr.vbs")

# SANITY CHECKS
If ($PrintersToRemove.Length -eq 0)
Write-Host "Required variable not filled! Please edit the script and re-run"
Exit 1

If (($PrintScriptFiles | ForEach Test-Path ($PrintingAdminScriptsFolder + "" + $_)) -contains $false)
Write-Host "Required scripts not found in specified folder! I need the two
VBS scripts ($(PrintScriptFiles)) inside of $($PrintingAdminScriptsFolder)
in order to function!"
Exit 2


# SCRIPT START
Write-Host "Starting at $(Get-Date -Format g) on client $($env:COMPUTERNAME)"
C:WindowsSystem32cscript.exe //H:CSCRIPT //S | out-null
cd $PrintingAdminScriptsFolder
Write-Host ""
Write-Host "Removing selected printers..."
$PrintersToRemove | foreach findstr "0x80041002"
Write-Host ""

If($PrintDriversToRemove.count -gt 0)
Write-Host "Removing selected Printer Drivers..."
$PrintDriversToRemove else
Write-Host "Removing excess Printer Drivers..."
.prndrvr.vbs -x







share|improve this question

















  • 1




    syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
    – Dannnno
    Apr 4 at 18:35










  • @CanadianLuke in order to function!" misses an introducing " character.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:43










  • @Dannnno PowerShell is not a supported language for the syntax highlighter.
    – 200_success
    Apr 4 at 19:45










  • @200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:46







  • 2




    @CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
    – Dannnno
    Apr 4 at 20:01
















up vote
0
down vote

favorite












I am learning Powershell Scripting as I move my work environment to Windows 10. The script below is to be edited by the SysAdmin (for the list of Print Queues and Printer Drivers) to specify what needs to be removed, then outputs what it did. I am looking for whether the Powershell syntax / convention is followed. This script will be distributed to other SysAdmins in my team when completed.



The script currently starts by declaring variables that will be used throughout the script. The variables tell the script which print queues to remove, and what print drivers to remove. It then loops through the arrays, removing the printers requested.



# Remove old printers by name - Written by Luke Barone
#
# VARIABLES TO EDIT
# $PrintersToRemove - (REQUIRED) An array of printers to cycle through and
# remove. Use the exact name of the printer to have it included in the list
# $PrintDriversToRemove - (optional) An array of printer drivers to cycle
# through and remove. You can obtain a list by running the "prndrvr.vbs -l"
# command on a computer with drivers to remove. Alternatively, you can
# leave the array empty, and the script will remove any unused (determined
# by the system) print drivers automatically.
$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

# The variables below should only be changed from the default if the scripts
# are not in this location!
$PrintingAdminScriptsFolder = "C:WindowsSystem32Printing_Admin_Scriptsen-US"
$PrintScriptFiles = @("prnmngr.vbs", "prndrvr.vbs")

# SANITY CHECKS
If ($PrintersToRemove.Length -eq 0)
Write-Host "Required variable not filled! Please edit the script and re-run"
Exit 1

If (($PrintScriptFiles | ForEach Test-Path ($PrintingAdminScriptsFolder + "" + $_)) -contains $false)
Write-Host "Required scripts not found in specified folder! I need the two
VBS scripts ($(PrintScriptFiles)) inside of $($PrintingAdminScriptsFolder)
in order to function!"
Exit 2


# SCRIPT START
Write-Host "Starting at $(Get-Date -Format g) on client $($env:COMPUTERNAME)"
C:WindowsSystem32cscript.exe //H:CSCRIPT //S | out-null
cd $PrintingAdminScriptsFolder
Write-Host ""
Write-Host "Removing selected printers..."
$PrintersToRemove | foreach findstr "0x80041002"
Write-Host ""

If($PrintDriversToRemove.count -gt 0)
Write-Host "Removing selected Printer Drivers..."
$PrintDriversToRemove else
Write-Host "Removing excess Printer Drivers..."
.prndrvr.vbs -x







share|improve this question

















  • 1




    syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
    – Dannnno
    Apr 4 at 18:35










  • @CanadianLuke in order to function!" misses an introducing " character.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:43










  • @Dannnno PowerShell is not a supported language for the syntax highlighter.
    – 200_success
    Apr 4 at 19:45










  • @200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:46







  • 2




    @CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
    – Dannnno
    Apr 4 at 20:01












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am learning Powershell Scripting as I move my work environment to Windows 10. The script below is to be edited by the SysAdmin (for the list of Print Queues and Printer Drivers) to specify what needs to be removed, then outputs what it did. I am looking for whether the Powershell syntax / convention is followed. This script will be distributed to other SysAdmins in my team when completed.



The script currently starts by declaring variables that will be used throughout the script. The variables tell the script which print queues to remove, and what print drivers to remove. It then loops through the arrays, removing the printers requested.



# Remove old printers by name - Written by Luke Barone
#
# VARIABLES TO EDIT
# $PrintersToRemove - (REQUIRED) An array of printers to cycle through and
# remove. Use the exact name of the printer to have it included in the list
# $PrintDriversToRemove - (optional) An array of printer drivers to cycle
# through and remove. You can obtain a list by running the "prndrvr.vbs -l"
# command on a computer with drivers to remove. Alternatively, you can
# leave the array empty, and the script will remove any unused (determined
# by the system) print drivers automatically.
$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

# The variables below should only be changed from the default if the scripts
# are not in this location!
$PrintingAdminScriptsFolder = "C:WindowsSystem32Printing_Admin_Scriptsen-US"
$PrintScriptFiles = @("prnmngr.vbs", "prndrvr.vbs")

# SANITY CHECKS
If ($PrintersToRemove.Length -eq 0)
Write-Host "Required variable not filled! Please edit the script and re-run"
Exit 1

If (($PrintScriptFiles | ForEach Test-Path ($PrintingAdminScriptsFolder + "" + $_)) -contains $false)
Write-Host "Required scripts not found in specified folder! I need the two
VBS scripts ($(PrintScriptFiles)) inside of $($PrintingAdminScriptsFolder)
in order to function!"
Exit 2


# SCRIPT START
Write-Host "Starting at $(Get-Date -Format g) on client $($env:COMPUTERNAME)"
C:WindowsSystem32cscript.exe //H:CSCRIPT //S | out-null
cd $PrintingAdminScriptsFolder
Write-Host ""
Write-Host "Removing selected printers..."
$PrintersToRemove | foreach findstr "0x80041002"
Write-Host ""

If($PrintDriversToRemove.count -gt 0)
Write-Host "Removing selected Printer Drivers..."
$PrintDriversToRemove else
Write-Host "Removing excess Printer Drivers..."
.prndrvr.vbs -x







share|improve this question













I am learning Powershell Scripting as I move my work environment to Windows 10. The script below is to be edited by the SysAdmin (for the list of Print Queues and Printer Drivers) to specify what needs to be removed, then outputs what it did. I am looking for whether the Powershell syntax / convention is followed. This script will be distributed to other SysAdmins in my team when completed.



The script currently starts by declaring variables that will be used throughout the script. The variables tell the script which print queues to remove, and what print drivers to remove. It then loops through the arrays, removing the printers requested.



# Remove old printers by name - Written by Luke Barone
#
# VARIABLES TO EDIT
# $PrintersToRemove - (REQUIRED) An array of printers to cycle through and
# remove. Use the exact name of the printer to have it included in the list
# $PrintDriversToRemove - (optional) An array of printer drivers to cycle
# through and remove. You can obtain a list by running the "prndrvr.vbs -l"
# command on a computer with drivers to remove. Alternatively, you can
# leave the array empty, and the script will remove any unused (determined
# by the system) print drivers automatically.
$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

# The variables below should only be changed from the default if the scripts
# are not in this location!
$PrintingAdminScriptsFolder = "C:WindowsSystem32Printing_Admin_Scriptsen-US"
$PrintScriptFiles = @("prnmngr.vbs", "prndrvr.vbs")

# SANITY CHECKS
If ($PrintersToRemove.Length -eq 0)
Write-Host "Required variable not filled! Please edit the script and re-run"
Exit 1

If (($PrintScriptFiles | ForEach Test-Path ($PrintingAdminScriptsFolder + "" + $_)) -contains $false)
Write-Host "Required scripts not found in specified folder! I need the two
VBS scripts ($(PrintScriptFiles)) inside of $($PrintingAdminScriptsFolder)
in order to function!"
Exit 2


# SCRIPT START
Write-Host "Starting at $(Get-Date -Format g) on client $($env:COMPUTERNAME)"
C:WindowsSystem32cscript.exe //H:CSCRIPT //S | out-null
cd $PrintingAdminScriptsFolder
Write-Host ""
Write-Host "Removing selected printers..."
$PrintersToRemove | foreach findstr "0x80041002"
Write-Host ""

If($PrintDriversToRemove.count -gt 0)
Write-Host "Removing selected Printer Drivers..."
$PrintDriversToRemove else
Write-Host "Removing excess Printer Drivers..."
.prndrvr.vbs -x









share|improve this question












share|improve this question




share|improve this question








edited Apr 4 at 20:02









Dannnno

5,3581649




5,3581649









asked Apr 4 at 17:15









Canadian Luke

4711417




4711417







  • 1




    syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
    – Dannnno
    Apr 4 at 18:35










  • @CanadianLuke in order to function!" misses an introducing " character.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:43










  • @Dannnno PowerShell is not a supported language for the syntax highlighter.
    – 200_success
    Apr 4 at 19:45










  • @200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:46







  • 2




    @CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
    – Dannnno
    Apr 4 at 20:01












  • 1




    syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
    – Dannnno
    Apr 4 at 18:35










  • @CanadianLuke in order to function!" misses an introducing " character.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:43










  • @Dannnno PowerShell is not a supported language for the syntax highlighter.
    – 200_success
    Apr 4 at 19:45










  • @200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Apr 4 at 19:46







  • 2




    @CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
    – Dannnno
    Apr 4 at 20:01







1




1




syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
– Dannnno
Apr 4 at 18:35




syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad
– Dannnno
Apr 4 at 18:35












@CanadianLuke in order to function!" misses an introducing " character.
– Ï€Î¬Î½Ï„α ῥεῖ
Apr 4 at 19:43




@CanadianLuke in order to function!" misses an introducing " character.
– Ï€Î¬Î½Ï„α ῥεῖ
Apr 4 at 19:43












@Dannnno PowerShell is not a supported language for the syntax highlighter.
– 200_success
Apr 4 at 19:45




@Dannnno PowerShell is not a supported language for the syntax highlighter.
– 200_success
Apr 4 at 19:45












@200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
– Ï€Î¬Î½Ï„α ῥεῖ
Apr 4 at 19:46





@200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine.
– Ï€Î¬Î½Ï„α ῥεῖ
Apr 4 at 19:46





2




2




@CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
– Dannnno
Apr 4 at 20:01




@CanadianLuke iirc PowerShell needs @" "@ for a multi-line string
– Dannnno
Apr 4 at 20:01










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:



PS> help printer

Name Category Module Synopsis
---- -------- ------ --------
Add-Printer Function PrintManagement Adds a printer to the specified computer.
Add-PrinterDriver Function PrintManagement Installs a printer driver on the specified computer.
Add-PrinterPort Function PrintManagement Installs a printer port on the specified computer.
Get-Printer Function PrintManagement Retrieves a list of printers installed on a computer.
Get-PrinterDriver Function PrintManagement Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort Function PrintManagement Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty Function PrintManagement Retrieves printer properties for the specified printer.
Read-PrinterNfcTag Function PrintManagement Reads information about printers from an NFC tag.
Remove-Printer Function PrintManagement Removes a printer from the specified computer.
Remove-PrinterDriver Function PrintManagement Deletes printer driver from the specified computer.
Remove-PrinterPort Function PrintManagement Removes the specified printer port from the specified computer.
Rename-Printer Function PrintManagement Renames the specified printer.
Set-Printer Function PrintManagement Updates the configuration of an existing printer.
Set-PrinterProperty Function PrintManagement Modifies the printer properties for the specified printer.
Write-PrinterNfcTag Function PrintManagement Writes printer connection data to an NFC tag.
Out-Printer Cmdlet Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:



$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)

Remove-PrinterDriver -Name $driver.Name -WhatIf



Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.



Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.



Instead of putting comments at the top of the script, you can use the PowerShell help system:



<#
.SYNOPSIS
Remove old printers by name

.DESCRIPTION
[Add stuff here if you like.]

.PARAMETER PrintersToRemove
(REQUIRED) An array of printers to cycle through and
remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
through and remove. You can obtain a list by running the "prndrvr.vbs -l"
command on a computer with drivers to remove. Alternatively, you can
leave the array empty, and the script will remove any unused (determined
by the system) print drivers automatically.

.NOTES
Written by Luke Barone

.EXAMPLE
Example 1

.EXAMPLE
Example 2
#>


That way you can do a help MyScript -detail



It's probably a good idea to use parameters to the script rather than getting people to edit it:



[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$PrintersToRemove,

[Parameter(Mandatory=$false)]
[string]$PrintDriversToRemove
)


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.



One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.



Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'





share|improve this answer























  • Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
    – joshgoldeneagle
    Aug 1 at 17:52






  • 1




    @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
    – Dangph
    Aug 3 at 8:31











  • Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
    – joshgoldeneagle
    Aug 3 at 19:58










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191263%2fpowershell-script-to-remove-printers-and-drivers%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:



PS> help printer

Name Category Module Synopsis
---- -------- ------ --------
Add-Printer Function PrintManagement Adds a printer to the specified computer.
Add-PrinterDriver Function PrintManagement Installs a printer driver on the specified computer.
Add-PrinterPort Function PrintManagement Installs a printer port on the specified computer.
Get-Printer Function PrintManagement Retrieves a list of printers installed on a computer.
Get-PrinterDriver Function PrintManagement Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort Function PrintManagement Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty Function PrintManagement Retrieves printer properties for the specified printer.
Read-PrinterNfcTag Function PrintManagement Reads information about printers from an NFC tag.
Remove-Printer Function PrintManagement Removes a printer from the specified computer.
Remove-PrinterDriver Function PrintManagement Deletes printer driver from the specified computer.
Remove-PrinterPort Function PrintManagement Removes the specified printer port from the specified computer.
Rename-Printer Function PrintManagement Renames the specified printer.
Set-Printer Function PrintManagement Updates the configuration of an existing printer.
Set-PrinterProperty Function PrintManagement Modifies the printer properties for the specified printer.
Write-PrinterNfcTag Function PrintManagement Writes printer connection data to an NFC tag.
Out-Printer Cmdlet Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:



$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)

Remove-PrinterDriver -Name $driver.Name -WhatIf



Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.



Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.



Instead of putting comments at the top of the script, you can use the PowerShell help system:



<#
.SYNOPSIS
Remove old printers by name

.DESCRIPTION
[Add stuff here if you like.]

.PARAMETER PrintersToRemove
(REQUIRED) An array of printers to cycle through and
remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
through and remove. You can obtain a list by running the "prndrvr.vbs -l"
command on a computer with drivers to remove. Alternatively, you can
leave the array empty, and the script will remove any unused (determined
by the system) print drivers automatically.

.NOTES
Written by Luke Barone

.EXAMPLE
Example 1

.EXAMPLE
Example 2
#>


That way you can do a help MyScript -detail



It's probably a good idea to use parameters to the script rather than getting people to edit it:



[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$PrintersToRemove,

[Parameter(Mandatory=$false)]
[string]$PrintDriversToRemove
)


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.



One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.



Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'





share|improve this answer























  • Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
    – joshgoldeneagle
    Aug 1 at 17:52






  • 1




    @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
    – Dangph
    Aug 3 at 8:31











  • Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
    – joshgoldeneagle
    Aug 3 at 19:58














up vote
3
down vote



accepted










Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:



PS> help printer

Name Category Module Synopsis
---- -------- ------ --------
Add-Printer Function PrintManagement Adds a printer to the specified computer.
Add-PrinterDriver Function PrintManagement Installs a printer driver on the specified computer.
Add-PrinterPort Function PrintManagement Installs a printer port on the specified computer.
Get-Printer Function PrintManagement Retrieves a list of printers installed on a computer.
Get-PrinterDriver Function PrintManagement Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort Function PrintManagement Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty Function PrintManagement Retrieves printer properties for the specified printer.
Read-PrinterNfcTag Function PrintManagement Reads information about printers from an NFC tag.
Remove-Printer Function PrintManagement Removes a printer from the specified computer.
Remove-PrinterDriver Function PrintManagement Deletes printer driver from the specified computer.
Remove-PrinterPort Function PrintManagement Removes the specified printer port from the specified computer.
Rename-Printer Function PrintManagement Renames the specified printer.
Set-Printer Function PrintManagement Updates the configuration of an existing printer.
Set-PrinterProperty Function PrintManagement Modifies the printer properties for the specified printer.
Write-PrinterNfcTag Function PrintManagement Writes printer connection data to an NFC tag.
Out-Printer Cmdlet Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:



$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)

Remove-PrinterDriver -Name $driver.Name -WhatIf



Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.



Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.



Instead of putting comments at the top of the script, you can use the PowerShell help system:



<#
.SYNOPSIS
Remove old printers by name

.DESCRIPTION
[Add stuff here if you like.]

.PARAMETER PrintersToRemove
(REQUIRED) An array of printers to cycle through and
remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
through and remove. You can obtain a list by running the "prndrvr.vbs -l"
command on a computer with drivers to remove. Alternatively, you can
leave the array empty, and the script will remove any unused (determined
by the system) print drivers automatically.

.NOTES
Written by Luke Barone

.EXAMPLE
Example 1

.EXAMPLE
Example 2
#>


That way you can do a help MyScript -detail



It's probably a good idea to use parameters to the script rather than getting people to edit it:



[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$PrintersToRemove,

[Parameter(Mandatory=$false)]
[string]$PrintDriversToRemove
)


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.



One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.



Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'





share|improve this answer























  • Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
    – joshgoldeneagle
    Aug 1 at 17:52






  • 1




    @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
    – Dangph
    Aug 3 at 8:31











  • Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
    – joshgoldeneagle
    Aug 3 at 19:58












up vote
3
down vote



accepted







up vote
3
down vote



accepted






Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:



PS> help printer

Name Category Module Synopsis
---- -------- ------ --------
Add-Printer Function PrintManagement Adds a printer to the specified computer.
Add-PrinterDriver Function PrintManagement Installs a printer driver on the specified computer.
Add-PrinterPort Function PrintManagement Installs a printer port on the specified computer.
Get-Printer Function PrintManagement Retrieves a list of printers installed on a computer.
Get-PrinterDriver Function PrintManagement Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort Function PrintManagement Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty Function PrintManagement Retrieves printer properties for the specified printer.
Read-PrinterNfcTag Function PrintManagement Reads information about printers from an NFC tag.
Remove-Printer Function PrintManagement Removes a printer from the specified computer.
Remove-PrinterDriver Function PrintManagement Deletes printer driver from the specified computer.
Remove-PrinterPort Function PrintManagement Removes the specified printer port from the specified computer.
Rename-Printer Function PrintManagement Renames the specified printer.
Set-Printer Function PrintManagement Updates the configuration of an existing printer.
Set-PrinterProperty Function PrintManagement Modifies the printer properties for the specified printer.
Write-PrinterNfcTag Function PrintManagement Writes printer connection data to an NFC tag.
Out-Printer Cmdlet Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:



$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)

Remove-PrinterDriver -Name $driver.Name -WhatIf



Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.



Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.



Instead of putting comments at the top of the script, you can use the PowerShell help system:



<#
.SYNOPSIS
Remove old printers by name

.DESCRIPTION
[Add stuff here if you like.]

.PARAMETER PrintersToRemove
(REQUIRED) An array of printers to cycle through and
remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
through and remove. You can obtain a list by running the "prndrvr.vbs -l"
command on a computer with drivers to remove. Alternatively, you can
leave the array empty, and the script will remove any unused (determined
by the system) print drivers automatically.

.NOTES
Written by Luke Barone

.EXAMPLE
Example 1

.EXAMPLE
Example 2
#>


That way you can do a help MyScript -detail



It's probably a good idea to use parameters to the script rather than getting people to edit it:



[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$PrintersToRemove,

[Parameter(Mandatory=$false)]
[string]$PrintDriversToRemove
)


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.



One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.



Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'





share|improve this answer















Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:



PS> help printer

Name Category Module Synopsis
---- -------- ------ --------
Add-Printer Function PrintManagement Adds a printer to the specified computer.
Add-PrinterDriver Function PrintManagement Installs a printer driver on the specified computer.
Add-PrinterPort Function PrintManagement Installs a printer port on the specified computer.
Get-Printer Function PrintManagement Retrieves a list of printers installed on a computer.
Get-PrinterDriver Function PrintManagement Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort Function PrintManagement Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty Function PrintManagement Retrieves printer properties for the specified printer.
Read-PrinterNfcTag Function PrintManagement Reads information about printers from an NFC tag.
Remove-Printer Function PrintManagement Removes a printer from the specified computer.
Remove-PrinterDriver Function PrintManagement Deletes printer driver from the specified computer.
Remove-PrinterPort Function PrintManagement Removes the specified printer port from the specified computer.
Rename-Printer Function PrintManagement Renames the specified printer.
Set-Printer Function PrintManagement Updates the configuration of an existing printer.
Set-PrinterProperty Function PrintManagement Modifies the printer properties for the specified printer.
Write-PrinterNfcTag Function PrintManagement Writes printer connection data to an NFC tag.
Out-Printer Cmdlet Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:



$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)

Remove-PrinterDriver -Name $driver.Name -WhatIf



Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.



Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.



Instead of putting comments at the top of the script, you can use the PowerShell help system:



<#
.SYNOPSIS
Remove old printers by name

.DESCRIPTION
[Add stuff here if you like.]

.PARAMETER PrintersToRemove
(REQUIRED) An array of printers to cycle through and
remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
through and remove. You can obtain a list by running the "prndrvr.vbs -l"
command on a computer with drivers to remove. Alternatively, you can
leave the array empty, and the script will remove any unused (determined
by the system) print drivers automatically.

.NOTES
Written by Luke Barone

.EXAMPLE
Example 1

.EXAMPLE
Example 2
#>


That way you can do a help MyScript -detail



It's probably a good idea to use parameters to the script rather than getting people to edit it:



[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$PrintersToRemove,

[Parameter(Mandatory=$false)]
[string]$PrintDriversToRemove
)


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.



One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.



Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'






share|improve this answer















share|improve this answer



share|improve this answer








edited Apr 5 at 2:42


























answered Apr 5 at 2:14









Dangph

1,458510




1,458510











  • Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
    – joshgoldeneagle
    Aug 1 at 17:52






  • 1




    @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
    – Dangph
    Aug 3 at 8:31











  • Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
    – joshgoldeneagle
    Aug 3 at 19:58
















  • Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
    – joshgoldeneagle
    Aug 1 at 17:52






  • 1




    @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
    – Dangph
    Aug 3 at 8:31











  • Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
    – joshgoldeneagle
    Aug 3 at 19:58















Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
– joshgoldeneagle
Aug 1 at 17:52




Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means?
– joshgoldeneagle
Aug 1 at 17:52




1




1




@joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
– Dangph
Aug 3 at 8:31





@joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that.
– Dangph
Aug 3 at 8:31













Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
– joshgoldeneagle
Aug 3 at 19:58




Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you.
– joshgoldeneagle
Aug 3 at 19:58












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191263%2fpowershell-script-to-remove-printers-and-drivers%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?