Logging Wscript.Network Setdefaultprinter method

Hello. I am trying to troubleshoot an issue with a line in a powershell logon script that maps a default printer for users in a certain group.

The code for this particular line I’m using is below:

How I can go about logging this piece of code to an output file? Tagging -verbose on the end doesn’t seem to work, start-transcript isn’t outputting anything and I tried using try/catch/finally but that didn’t work either.

Any help would be appreciated.


if ($usergroup -contains $env:Username) {(New-Object -ComObject WScript.Network).SetDefaultPrinter('\\servername\printername')}

This is not enough info to assist you.
You need to show your code and the input you are using.
The fact that this is working with some and not others, specifically says, there is something wrong with the input relative to the ones that are failing and or something on the host where you are setting the printer. So, this could be both input and environmental. The environmental part would be a challenge for us to assist with.

Hi postanote.

To clarify, my question is regarding the logging of the particular line of code in my post.

That is all I am looking for assistance with (basically it gives me no output to work with that I can see, tagging -verbose on the end doesn’t appear to work). The rest of the post was just for context however I’ve edited my original post to better reflect what I’m asking to eliminate confusion.


Did you disable windows 10 handling the default printer?

Hi js. Yes I did do this.

To clarify, my question is simply related to how to obtain output for logging purposes. So far I am unable to use -verbose, out-file outputs nothing, nor have I been successful with try/catch, or tee.

Hope that clarifies.


Hmm, I can’t figure out what stream the error message is in. $? will be $false with an error right after it though.

Another way to set the default printer. Maybe you can troubleshoot this easier.

$printerObject = Get-WmiObject win32_printer | where name -eq \\$server\$defaultprinter
$result = $printerObject.SetDefaultPrinter()
"default \\$server\$printer $($result.returnvalue)"

Hi JS. Thanks for the info. I’ll give that a shot and see how that works. I originally was trying to use the wscript.network setdefaultprinter method outlined here:



Where are you placing you error handling code?

So, you are saying, whether you get success or failure on any host, you are not seeing any response from your code?

Meaning, if you ran your code outside of the script.

So, step away from the full script, and test only the piece you are trying to gain info on.

Try taking the script using one of these approaches. One you have already used.

Tracing the Execution of a PowerShell Script https://devblogs.microsoft.com/scripting/tracing-the-execution-of-a-powershell-script

Trace Your Commands by Using Trace-Command

Again, this is not specifically PS, you are using COM, and you are asking COM to return information about that state of the request.

Try using different approach to see what results you get. Try this one of these scripts instead.

How to set default printer using PowerShell This PowerShell script shows how to set default printer in Windows. https://gallery.technet.microsoft.com/scriptcenter/How-to-set-default-printer-cf89670b

Set default printer with PowerShell

Or use .Net vs COM

$Printer = 'SomePrintName'
($PrintClass = [wmiclass]'win32_printer')


# Example0:
## get the printer and Set the printer as default
$printer = Get-WmiObject -Query "Select * From Win32_Printer Where Name = 'name of the printer'"

# Example1:
$AvailablePrinters = Get-WmiObject -ClassName Win32_Printer
($AvailablePrinters | Where-Object -FilterScript {$_.Name -eq "Printer Name"}).SetDefaultPrinter()

$AvailablePrinters = Get-CimInstance -ClassName Win32_Printer
$AvailablePrinters | 
Where-Object -FilterScript {$_.Name -eq "Printer Name"} | 
Invoke-CimMethod -ClassName Win32_Printer -MethodName SetDefaultPrinter

# Example3:
(Get-WmiObject -Class Win32_Printer -Filter "Name='$name'").SetDefaultPrinter()

Or step away from
New-Object -ComObject WScript.Network and use rundll32 printui.dll,PrintUIEntry

Also of note:

Windows 10 uses by default the last chosen printer as default
If you need to specify a persistent default printer you can disable this feature with the following registry key:

Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force

Thanks for the info postanote. That was helpful to learn all of that, it should come in handy.

I was actually able to narrow down the issue by pointing the logfile created by start-transcript to the users $home\Desktop folder (due to access restrictions as it’s on an RDS). I also enabled tracing and debug but ended up not needing it.

From there I found that it was actually unable to load the active-directory module (due to being 2008R2).