Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3930
  • Last Modified:

WMI Script to change the password for local user account

I need a WMI script to change a password for local user account. (Will be running this script against multiple computers at the same time)
- servers are Not active directory
- I want to accomplish this using WMI scripting
- Script output should indicate success/failure of the job
Appreciate any help.

I am a sysadmin with little expereince in WMI scripting. Tried Microsoft script fom technet, does not slove my purpose
  • 4
  • 2
  • 2
1 Solution
Pete LongTechnical ConsultantCommented:
skn73Author Commented:
For a VB Solution see

I tried this script before i posted my question. Did not solve my purpose

Program: ResetPW.EXE: Remote Password Change Utility

I want to use WMI scripting.

Thanks for your time in posting the comment though.
Pete LongTechnical ConsultantCommented:
OK Sorry I couldnt be more help - good luck

if you get a working solution post back and let us know :)

best wishes
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

changes local administrator if you know the group exchange for group you would like to change

strComputer = "MyComputer"
Set objUser = GetObject("WinNT://" & strComputer & "/Administrator, user")
objUser.SetPassword "testpassword"

this link gives lots of user scripts for wmi
skn73Author Commented:
Sorry Scoot, We wrote the script ourselves My coworkers helped me.
 Here is the script that worked.

' Description: Accepts entry of username and password and changes the password for that
'              username on the supplied list of computers.
' Requirements: none
' Creation Date: 08-17-2004
' File Type: vbs
' Version:      1.0 original release

Option Explicit

Dim objFSO 'file system object

Dim WshShell 'windows scripting host shell

Dim objTextFile 'text file object

Dim objUser 'user object

Dim strUserName 'user name to modify

Dim strPassword 'new password to set

Dim arrComputers 'computer array from file

Dim strComputer 'single computer

Dim strFileName 'filename to read

Dim strLogFileName 'filename to write

Dim intCount 'counter

Const ForWriting = 2 'creates a new text file for writing

Const ForAppending = 8 'appends to a text file

strFileName = "computers.txt"

'change this to whatever you want to name the file
strLogFileName = "PasswordChangeResults.csv"

'Shell object
Set WshShell = WScript.CreateObject("WScript.Shell")

'file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'puts the text file in the same directory where the script is executed
Set objTextFile = objFSO.OpenTextFile (WshShell.CurrentDirectory & "\" & strLogFileName, ForWriting, True)

objTextFile.writeline "Computer,Status"

'get the username
strUserName = inputbox ("Enter username for password change","Username Entry")

'get the password
strPassword = inputbox ("Enter new password","Password Entry")

If (strUserName = "" Or strPassword = "") Then

      msgbox "Username and password must contain data.  Please run the script again."
End If

'read text file into an array skipping blanks
arrComputers = GetTextFileLines(strFileName,True)

'go through each item in the array
For intCount = 0 To ubound (arrComputers)

      strComputer = trim(arrComputers(intcount))

      'ping the computer first
      If (ping(strComputer)=True) Then      
            'get the WMI object
            On Error Resume Next

            Set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName & ",user")

            If (err.number = 0) Then
                  objUser.SetPassword strPassword

                  If (err.number = 0) Then
                        On Error goto 0
                        objTextFile.writeline strComputer & "," & "Success"
                        On Error goto 0
                        objTextFile.writeline strComputer & "," & "Unable to set password, error number " & err.number

                  End If
                  'no user exists or not access
                  On Error goto 0
                  objTextFile.writeline strComputer & "," & "No user exists or you do not have access to " _
                        & "this user, Error number " & err.number
            End If

            'can't ping the machine
            objTextFile.writeline strComputer & "," & "No Ping Response"
      End If      

Next 'arrComputers

'cleanup and close

Set objUser = Nothing


Set objFSO = Nothing

Set WshShell = Nothing

Set objTextFile = Nothing

msgbox "done"

' Description: This function pings a specified computer and returns true if ping is a success.
' Requirements: none
' Creation Date: 10-31-2002
' Author: from -

Function Ping(strHost)
  Dim objShell 'shell object
  Dim objStdOut 'std out to read
  Dim strLine 'line to read
  Dim objRegExp 'expression to examine
  Dim strPattern 'pattern to look for
  Dim wshShell
  Set WshShell = CreateObject( "WScript.Shell" )

  Ping = False

  Set objShell = WshShell.Exec("PING.EXE " & strHost)  'The command line
  Set objStdOut = objShell.StdOut 'Reads the output from the command
  Set objRegExp = New RegExp
  objRegExp.IgnoreCase = True

  Do While Not objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    strLine = Trim(strLine)
    'Test for "Unknown Host" - DNS Failed
    objRegExp.Pattern = "^Unknown"
    If objRegExp.Test(strLine) Then
      Ping = False
      Exit Function
    End If

    'DNS Succeeded, but box off-line
    objRegExp.Pattern = "100% loss"
    If objRegExp.Test(strLine) Then
      Ping = False
      Exit Function
    End If

    'Got a Reply From,
    objRegExp.Pattern = "^Reply from"
    If objRegExp.Test(strLine) Then
      'Check for "Destination host unreachable."
      objRegExp.Pattern = "Destination host unreachable\."
      If objRegExp.Test(strLine) Then
        Ping = False
        'S/B OK here
        Ping = True
      End If
      Exit Function
    End If

  Set objShell = Nothing
  Set objStdOut = Nothing
  Set objRegExp = Nothing
End Function

' ----------------------------------------------------------------
' Function: Read text file and load into array
' Description: Used to read a text file into an array.
' Example: GetTextFileLines("c:\temp\test.txt",True)
' The example reads the test.txt file in the c:\temp directory into an array without the blank lines.
' Requirements:
' Creation Date: 10-23-2003
' Author: Tim McGue inspired by -
' ----------------------------------------------------------------

Function GetTextFileLines(strFileName,bolDropEmpty) 'parameters: file name to read, boolean to drop blank lines

Dim objFSO 'file system object

Dim intCount 'counter

Dim objTextStream 'text stream object

Dim arrLinesRead 'array of the lines read from the text file

'set variables
Set objFSO = CreateObject("Scripting.FileSystemObject")

'ensure the file exists
If Not (objFSO.fileexists(strFileName)) Then

      msgbox "The file " & strFileName & " does not exist."
      Exit Function

End If

'Open a text stream
Set objTextStream = objFSO.OpenTextFile(strFileName)

'read the entire file
arrLinesRead = objTextStream.ReadAll

'split on CRLF
arrLinesRead = split(arrLinesRead,vbCrlf)

'go through each line in the array
For intCount = 0 To ubound(arrLinesRead)

      'trim each line
      arrLinesRead(intcount) = trim(arrLinesRead(intcount))
      'if the line is blank replace it with a CRLF character
      If (arrLinesRead(intcount) = "") Then
            arrLinesRead(intcount) = vbCrlf
      End if


If (bolDropEmpty) Then 'need to remove blanks

      'cleanup blank lines on CRLF
      arrLinesRead = filter(arrLinesRead,vbCrlf,False)

End If

'return the array
GetTextFileLines = arrLinesRead


Set objFSO = Nothing

Set objTextStream = Nothing

End Function
nice i am lazy why not query the domain and create a list of computers of your domain listing

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set tsr = fso.OpenTextFile("COMPUTER.txt", 1) 'computers on the domain for reading
   Set tst = fso.OpenTextFile("COMPUTER.txt", 2) 'computers on the domain for writing to
   Set tss = fso.OpenTextFile("server.txt", 2) 'list of servers on the network for writing to

on error resume next
query domain for list of all computers ont the domain

sDomain       = "YOUR DOMAIN"
Set oDomain       = GetObject("WinNT://" & sDomain)
oDomain.Filter       = Array("Computer")
'filter out all servers on the domain the servers naming convention is mkc_ or mkc-
      For Each oADobject In oDomain
            if instr(oADobject.Name, "SERVERNAMAING CONVENTION" ) then
                  tss.write oADobject.Name
                  tss.write vbcrlf
            elseif instr(oADobject.Name, "OTHERSERVERNAMING CONVENTION" ) then
                  tss.write oADobject.Name
                  tss.write vbcrlf
                  tss.write oADobject.Name
                  tss.write vbcrlf
            elseif instr(oADobject.Name, "PCNAMING CONVENTION" ) then
                  tst.write oADobject.Name
                    tsT.write vbcrlf
            end if
      odomain = nothing


the server naming convention to filter out any servers from the list
pcnaming is the list of pcs you want to include

for instance in pc naming all the PC in our domain are "XYZ" and a four digit number

so we capture the text file with all XYZ so add xyz where pc naming convention is

but we don't want to run this on servers so all servers have "XYZ"_digit or name
so we add "xyz_" in server naming and filter out those

also we have servers with "xyz-" as naming convention so we will filter those out to server text so we only update workstations

now you can get the script to query the domain and build a list of computers itself.

but I would also amend the script to check to see if computer exists and is empty if empty run the domain query and populat the computer.txt then towards the end of the script remove any successful computer names from the computer.txt

so that it only queries the domain and does it once for a list of pc's

then when you run iot again it doesn't recreate the list of computers.

then I'd get it to run the script to ping and if succesfull change the password on that pc.

Then after it has done that remove the computer from the computer.text file so it doesn't ping or query for that pc again.

so you can keep on running that script until it has finnally done all the machines in the list. knowing that your not running it against the whole list again and again and you will know which machines it has run against and which need to be powered up.
we used a similar script to that to query the domain and build a list from wmi of all the dell serial numbers and what computer name the sersial number beleonged to for an audit as we had on our pc inventory all the computer names and who they belonged to but when leased the pc's only had serial numbers to identify which ones went back so when the leasing company said we want these serials we had to match a serial to computer name. An oversight on the managers part just list the PC's boy and get them on the desk top.

Oh that was fun as we have 4,000 plus pcs we has to automate it. hope your script works well the problem with the ping method was tyhe time it takes to run .

say you have 4000 pcs each ping is three requests each request takes say on average 60 miliseconds so each pc will need 1.5 seconds per request x 3 equals 4.5 seconds per machine to ping back plus the time it takes to run other parts of the script say half a second so 5 seconds per machine times 4000 so this would take 5.5 hours to run approx so i would filter out any successful attempts so it would only run the script on the machines that it needed.

don't make the same mistake we did :)

when we first wrote it we didn't filter out so thius script would run from begining of the day to the end for a few days before we decided to filter out. OOPPSSS

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now