Create automated response when prompted.

I'm trying to make things easier by having end users just run a script to automatically set "\\prtcen\SmartPrint" (Network printer on our domain) as the default printer. I got a script online thats similar, but it prompts me to type the printer name. I'm fairly new to powershell and I'm unsure of how I will be able to set an automatic response for the printer. please help.

#>


[CmdletBinding(SupportsShouldProcess = $true)]
Param
(
[Parameter(Mandatory=$true)]
[String]
$PrinterName
)

#Get the infos of all printer
$Printers = Get-WmiObject -Class Win32_Printer

If($PSCmdlet.ShouldProcess("$PrinterName","Set the default printer"))
{
Try
{
Write-Verbose "Get the specified printer info."
$Printer = $Printers | Where{$_.Name -eq "$PrinterName"}

If($Printer)
{
Write-Verbose "Setting the default printer."
$Printer.SetDefaultPrinter() | Out-Null
EHCO "\\prtcen\SmartPrint"

Write-Host "Successfully set the default printer."
}
Else
{
Write-Warning "Cannot find the specified printer."
}
}
Catch
{
$ErrorMsg = $_.Exception.Message
Write-Host $ErrorMsg -BackgroundColor Red
}
}

Parents
No Data
Reply
  • Why are you doing this a user interactive script vs GPO, take the user interaction out of the picture, no coding needed?

    Just web search for --- 'gpo set default printer', and get stuff like this...

     GP Preferences: Add a new printer, set as default
     https://blogs.technet.microsoft.com/grouppolicy/2009/06/24/gp-preferences-add-a-new-printer-set-as-default

     Videos of gpo set default printer
     https://www.bing.com/videos/search?q=gpo+set+default+printer&qpvt=gpo+set+default+printer&FORM=VDRE

    Don't try an reinvent the wheel, unless you know it is really a better wheel, or a learning process.

    We all love PowerShell, but there are thing already built into Windows to do this sort of thing.

    PowerShell can do a tremendous amount of things, but it is not the default answer to everything, especially for one off items like what you appear to be after here.


    As for this...

    "I'm fairly new to powershell"

    ... that's all well and good. We all had to start from somewhere., but... Spend the needed time learning this stuff, to limit, avoid, bad code, misconceptions, unnecessary errors, bad habits and the like. Don't stress yourself, take advantage of all the free / no cost resources available.

    Example(s).

    There is a MS MOC, and there are online courses.
    Yet there are many free / no cost approaches.
    Youtube (Microsoft MSDN Channel9 has a bunch of PowerShell items as well. ) - and just search ---
    'beginning powershell'
    'intermediate powershell'
    'advanced powershell'
    'PowerShell using Active Directory'
    'powershell Powergui'
    'Powershell working with files and folders'
    .. yadda, yadda.


    There are tons of free / no cost / low cost, eBooks and references.
    powertheshell.com/cookbooks
    blogs.technet.microsoft.com/pstips/2014/05/26/free-powershell-ebooks
    rkeithhill.wordpress.com/2009/03/08/effective-windows-powershell-the-free-ebook
    veeam.com/wp-powershell-newbies-start-powershell.html
    reddit.com/r/PowerShell/comments/3cki73/free_powershell_reference_ebooks_for_download

    This question gets asked a lot on many forums and Q&A sites. Bascailly, stop using cmd.exe, and alwqays use PowerShell for everything you do, where you'd use cmd.exe and do things only using the equivalent PowerShell cmdlet. Use the help files and the help file examples to get going.

    See also posts here for other resource suggestions here:
    Learning this stuff.
    https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eooduq9/?context=3
    https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eoodxzu/?context=3
    Best Practices
    https://www.reddit.com/user/get-postanote
    Passwords
    https://www.reddit.com/r/PowerShell/comments/bv7ywa/whats_the_best_practice_for_passwords_in_ps/epoux2c/?context=3
    https://www.reddit.com/r/PowerShell/comments/c5qbjb/how_to_store_password_in_powershell_file/

    Windows PowerShell Survival Guide
    social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

    Practice with PSKoans
    PSKoans : 0.50.0
    A module designed to provide a crash-course introduction to PowerShell with programming koans.
    https://www.powershellgallery.com/packages/PSKoans/0.50.0

    Book references, normally the ones you'll see most recommend:

    Beginning ---
    Learn Windows PowerShell in a Month of Lunches 3rd Edition
    Donald W. Jones (Author),‎ Jeffrey Hicks (Author)
    ISBN-13: 978-1617294167
    ISBN-10: 1617294160

    Internediate ---
    Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft's Command Shell 3rd Edition
    Lee Holmes (Author)
    ISBN-13: 978-1449320683
    ISBN-10: 1449320686

    Advanced ---
    Windows PowerShell in Action 3rd Edition
    by Bruce Payette (Author),‎ Richard Siddaway (Author)
    ISBN-13: 978-1633430297
    ISBN-10: 1633430294

Children
No Data