Pages

Search

Wednesday, September 19, 2012

Changing Screen Resolution with Powershell

It's been a while since I have had time to post anything of interest to me but I wanted to share a little bit of information I stumbled across a few days ago. The situation required a log on script to change screen resolution to a pre-defined size. How would I accomplish this task without using a program like qres or reso and make a script to do it?

I like PowerShell scripts because it seems to do just about anything on a Windows pc. After a short search I turned towards a resource that has been a great resource in my personal PowerShell journey, Microsoft's "Hey, Scripting Guy!" blog & Script Center. I found an interesting article from two years ago asking the very same question I had, with an interesting answer, and a example that at first wasn't clear to me how it worked. I plan to explain it in a way that may help other's still learning PowerShell.

After reading the "Hey, Scripting Guy!" blog entry from July 7th, 2010 I copied and pasted the code into my favorite text editor, notepad++ , and began to analyze the code. What was scripted by Andy Schneider was exactly what I had been looking for!

Here's where I screwed up at first; I didn't catch that the copied code was essentially a custom PowerShell function that needs to be called upon when loaded at the powershell command prompt. If you remove the "Function Set-ScreenResolution" and curly braces from the top and bottom of the script, you will get a simple GUI asking for resolution parameters.

I have outlined the steps below with screenshots to create a PowerShell script that calls this custom function and based on your input, will change your screen resolution.

1. Create a folder in a location of your choosing and call it "ScreenResolution" or whatever you choose.
2. Using your favorite PowerShell editor (I used both notepad++ and PowerShell ISE), copy and paste the code example from Andy Schneider listed in the above "Hey, Scripting Guy!" post from July 7th, 2010 into your new .ps1 file. Save this function as "Set-ScreenResolution.ps1" .
3. Open a new tab in your powershell editor, and use my example PowerShell script below:








Save this script. I chose 1024x768.ps1 to identify the resolution the script would set as my resolution.
*Notice the . before the path to our function. This loads the function before executing the command with parameters. This is where I had some trouble at first, thought I would note it. For more information on Functions, here's a good start: http://www.powershellpro.com/powershell-tutorial-introduction/powershell-functions-filters/
4. Open a PowerShell prompt and cd to your folder location. In my example, I chose C:\ScreenResolution\.

All that's left is to type in the name of your resolution PowerShell script containing your set parameters and voila! screen resolution changes! You can also right click on the script and choose "Run with PowerShell".

I hope this helps those like me who are not programmers but enjoy learning and using PowerShell. I would also like to say thank you to the whole "Hey, Scripting Guy!" team for the original blog post about this topic.