Generate random password with PowerShell

So I was updating the PowerGUI AD pack the other day and wanted to add random password generation function for Reset Password action.

Here’s the code I came up with:

#Set up random number generator
$rand = New-Object System.Random
#Generate a new 10 character password
1..10 | ForEach { $NewPassword = $NewPassword + [char]$rand.next(33,127) }
#Reset the password
Set-QADUser "Dmitry Sotnikov" -UserPassword $NewPassword

That’s it. I guess I could get more fancy by ensuring that characters from 3 out of 4 character groups are present, etc. but this was the easiest one-liner to keep me going.

Tags: , , , , , , , ,

About these ads

8 Responses to “Generate random password with PowerShell”


  1. 1 Adam Bell July 18, 2007 at 11:23 am

    HI Dimitry,

    I have a function thats not as simple as yours, but does generate complex passwords. It’s filed under the less than obvious post:
    http://www.leadfollowmove.com/archives/powershell/powershell-less-code-same-result

    I was comparing the same function between VBS and PoSH, and used a password gen function as my sample.

    Cheers

    Adam :)

  2. 2 dmitrysotnikov July 18, 2007 at 7:22 pm

    Hey Adam! Thanks for the link. I agree that technically my oneliner might in some cases fail to generate a complex password but I wanted it so much to be a oneliner! ;)

    Anyway, I’m glad you posted a link to your script so folks can find an alternative while reading this post.

    Dmitry

  3. 3 Simpler yet! July 29, 2007 at 5:10 am

    Here’s an even simpler call for password generation

    If you already have the assembly loaded, you can skip the first line:
    [Reflection.Assembly]::LoadWithPartialName(“System.Web”)

    Then it really is a one-liner

    [System.Web.Security.Membership]::GeneratePassword(10,2)

  4. 4 dmitrysotnikov July 29, 2007 at 6:53 am

    Wow! Yes, it looks like you can use the method standalone without other Membership functionality, so yes, great tip! Learning something new every day.

  5. 5 shanmugam.uthayakumar@wipro.com March 10, 2011 at 3:45 pm

    hi team,
    We are changing the IIS server password every 90 days once.can you please help me to automate the process of changing random password for more than 100 IIS Servers(Windows 2003) using POWERSHELL.

    Internet information services-.server name->application pools->default application pool->properties->identity->confiurable->user name:- abc
    password:- abc

    Thanks in Advance
    Shanmugam.u

  6. 6 Robbie May 13, 2011 at 8:42 am

    Is there any method of randomizing a alphabetical character only password?

    Thanks

    Robbie.

  7. 8 alex April 18, 2013 at 7:50 am

    watch out you probably shouldn’t use System.random as the random number generator especially if you are planning to use it on very many accounts:
    on MSDN
    “Random Initializes a new instance of the Random class, using a time-dependent default seed value.” So the more precisely a potential attacker knows of the time at which the passwords where generated the smaller the search space.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




My Recent Tweets

RSS My company’s blog

  • Fun at HostingCon 2013
    The smell of the hosting industry’s finest is in the air, and of course we sent some of the Jelastic team to attend and proudly exhibit at HostingCon 2013 this week in Austin, Texas.  From what I have been reading, there are more than 50 scheduled sessions from Sales and Marketing, Technology, Issues and Trends and [...]The post Fun at HostingCon 2013 appear […]
  • Secure Access to Your Jetty Web Application
    Today’s post focuses on the web application security related features of Jetty app server. After reading this article you should be able to configure security realms to provide authentication and access control for your Jetty web application, as well as to grant access to your app for dedicated IP-addresses only. Before we start let’s examine what Jetty real […]
  • Software Stacks Market Share: May 2013
    We are back to update you with the latest trends in software stacks popularity for May 2013. This time we decided to collect the numbers in a different way to get more accurate statistics. As you remember previously we counted the number of the environments, where each software stack was used. We’ve changed the reporting [...]The post Software Stacks Market […]
  • Setting Up a Cronjob in Jelastic Cloud
    Cronjob allows you to configure regularly scheduled tasks so that the jobs can be run automatically at a pre-set point of time. It repeats itself and does not need any regular manual instructions. Cron automates your system and can be used for quite different purposes. This wonderful tool is a standard part of all sysadmins toolkit. Also cronjob has a [...]T […]
  • How to Deploy Joget Cluster into the Cloud
    Joget Workflow is an open source platform that allows you to build enterprise web applications easily, due to its rich set of tools. It is also a rapid application development platform that provides complete agile development capabilities, including consisting of processes, forms, lists, CRUD and UI; not just back-end EAI/orchestration/integration or the tas […]
  • Liferay Cluster in the Cloud
    Liferay Portal is one of the most popular Java CMSs in the world due to its impressive ease-of-use. Since we published the tutorial on Liferay deployment to the cloud we have seen an extremely positive reaction from its community. Also we have received multiple requests from Liferay fans asking about clustering, replication and fail-over capabilities in the […]
  • Mark Zbikowski Veteran Microsoft Architect Joins Jelastic
    Jelastic already has an impressive team of advisers including Serguei Beloussov, the founder of Parallels. We also have technical geniuses who endorse and use Jelastic including the father of Java James Gosling, David Blevins who founded the TomEE project, and Michael “Monty” Widenius, the author of the original version of the open-source MySQL database and […]

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my former employer - Quest Software, or my current employer - Jelastic or anyone else for that matter. All trademarks acknowledged.

© 2007-2013 Dmitry Sotnikov

Pages

July 2007
M T W T F S S
« Jun   Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: