0

My main goal is to print a .PDF file through PowerShell.

I can do that with the following line Start-Process $Path -Verb Print, so long as the user's default app for .PDF files is Adobe Acrobat.

So, in order to achieve printing from PowerShell, this is my intended script pseudocode:

- Get user's default app for .pdf
- Set user's default app for .pdf to Adobe Acrobat
- Print the .pdf file
- Set the user's default app for .pdf back to whatever they had it set to originally

But I cannot figure out how to do this. The closest I have gotten is the ftype Command Prompt command, but here is what I have witnessed that makes that not a solution:

When my default app is Adobe, print works. When my default app is Edge, print does not work, but the return from ftype is exactly the same as when my default was Adobe.

enter image description here

Does anyone know how to get/set default app through PowerShell?

2 Answers 2

0

I recommend DanySys's Set-FTA (file type association) for this:

Set-FTA AcroExch.Document.DC .pdf

However, if you just want to print the files through adobe reader, try the command for that:

# Open and go straight to the print dialog
AcroRd32.exe /p <filename>
1
  • Thank you for the AcroRd32 command. I did have to use the full path starting at C:\ since I guess Adobe isn't in my PATH, but nonetheless this is very helpful. Thank you!
    – Tyler N
    Mar 1, 2021 at 18:12
0

Okay, it appears that changing the default app for .pdf in the Settings will set values in

Computer\HKEY_USERS\[USER-ID]\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pdf\UserChoice

So, here is a PowerShell script that gets the value for the user's set default app for .PDF

$User = New-Object System.Security.Principal.NTAccount($env:UserName)
$sid = $User.Translate([System.Security.Principal.SecurityIdentifier]).value
$key = "HKU:\${sid}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pdf\UserChoice"
if (Get-PSDrive HKU -ErrorAction SilentlyContinue) {
} else {
    New-PSDrive HKU Registry HKEY_USERS
}
(Get-ItemProperty -Path $key).ProgId

This enables me to get the name of the default, and If I go into RegEdit, and remove the Deny permission of Set Value of the key for my user then I can set it, but by default the user's permission is denied for this key.

1
  • Brilliant. I am going to suppose that this may be applied to other extensions as well. Thank you. Jan 6 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .