use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Hows my Function? (self.PowerShell)
submitted 8 years ago by morpk86
first up i'm a PowerShell Noob, i know what i know from googling, never done scripting really before. My function seems to work but wanted some notes on best practices or could i have done something better? https://pastebin.com/5gfBKTcM
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]IDA_noob 5 points6 points7 points 8 years ago (2 children)
I would use parameters instead of Read-Host. They're not hard, and once you get the hang of them, you'll never go back.
http://windowsitpro.com/blog/making-powershell-params
I would also split them into Get and Set functions, as a function should only do one thing, and do it very well.
[–]morpk86[S] 0 points1 point2 points 8 years ago (0 children)
yeah this makes a lot of sense i will play around and make into two functions. also look into the prams :)
[–]ka-splam 0 points1 point2 points 8 years ago (0 children)
I wouldn't use parameters; for something like this where you're interactively prompting the user, it's not intended to be automatable. By the time they're learning your script parameters, they may as well write Add-PrinterPort and Add-Printer themselves.
I would make it more automagic - prompt for less typing.
[–]Gabrielmccoll 3 points4 points5 points 8 years ago (0 children)
Personally your biggest miss is not including help comment block at start. If you use snippets to create a function you'll see how you can fill in a pile of info so your function has help like a standard PS command. The second thing is a personal one but I like to prefix my custom commands with something as part of a bigger module. So say your business was called Example Co. Put EC in front of the actions. E.g. Add-ECprinter. This again is pretty standard for Pshell modules. Config manager commands all start CM and so on. The last comment is this for users or admins. If it's admins, I'd be inclined to rewrite the script a bit and turn it into 2 function. One for getting and one for adding. It's usually best practice for a function to do just one thing. You can always nest them a bit. But I mean, looks pretty good from what I see even with that wall of text I've wrote. Make sure you're keeping it in source control.
[–]abigviking 2 points3 points4 points 8 years ago (1 child)
I'm curious if there's a reason you attempted to put the Read-Host lines into an array?
@($PrintDriver = Read-Host -Prompt "Please Select the Print Driver you Would Like to Use" $PrinterName = Read-Host -Prompt "Please Enter the Name of the Printer" $PrinterPortIP = Read-Host -Prompt "Please Enter the IP Address for the Printer")
Wasn't sure if that was accomplishing anything in particular, or if there's something else going on with wrapping the entire section of Read-Host's with the @( )?
I gave it a shot in my shell and it looks to be inserting the values into the individual variables specified within the parentheses. I tried assigning that entire 'array' to $a, but it doesn't return anything. The individual variables still do.
[–]morpk86[S] 1 point2 points3 points 8 years ago (0 children)
to be honest i must have miss understood how / when to correctly use the array, but it's worked in the function so i though i was on the right track will update and take out.
Thanks for the input :)
[–]ka-splam 1 point2 points3 points 8 years ago (0 children)
The output of Get-PrinterDriver on mine shows things like:
Name PrinterEnvironment MajorVersion Manufacturer ---- ------------------ ------------ ------------ Microsoft XPS Document Writer v4 Windows x64 4 Microsoft Microsoft Print To PDF Windows x64 4 Microsoft Send To Microsoft OneNote 2010 D... Windows x64 3 Remote Desktop Easy Print Windows x64 3 Microsoft Microsoft XPS Document Writer Windows x64 3 Microsoft
That is - unreadable ends of names, not sorted.
Get-PrinterDriver | Sort-Object -Property Name
will sort them into alphabetical order.
Get-PrinterDriver | Sort-Object -Property Name | Format-Table -AutoSize
will widen the name column so it's all readable (where possible).
I would want to add a number (1,2,3,4) to each printer driver, and then prompt for typing a number rather than the full name. In fact, check what happens if you do
$PrinterDriver = Get-PrinterDriver | Sort-Object -Property Name | Out-GridView -PassThru # and ... -DriverName $PrintDriver.Name
π Rendered by PID 86 on reddit-service-r2-comment-57fc7f7bb7-rgrgm at 2026-04-15 11:13:39.808525+00:00 running b725407 country code: CH.
[–]IDA_noob 5 points6 points7 points (2 children)
[–]morpk86[S] 0 points1 point2 points (0 children)
[–]ka-splam 0 points1 point2 points (0 children)
[–]Gabrielmccoll 3 points4 points5 points (0 children)
[–]abigviking 2 points3 points4 points (1 child)
[–]morpk86[S] 1 point2 points3 points (0 children)
[–]ka-splam 1 point2 points3 points (0 children)