Use PowerShell to shave time off routine tasks like renaming computers. Here are some commands to try.
Occasionally
it may become necessary to rename a computer in an Active Directory
environment. Using the System applet in the Control Panel is the way
many administrators have been solving this issue for a long time. Until
recently I didn’t think much about the work actually involved in
changing a computer name. The typical steps are:
- Log on to the workstation.
- Access the control panel.
- Open the System applet.
- Select the change option to change the PC name or domain membership.
- Specify a new computer name.
- Click OK to save the change.
- Click OK on the main properties dialog.
- Restart the computer.
Depending
on the number of things happening when the computer starts, going the
long way (or through the Windows GUI) can take anywhere from 2 – 5
minutes. For one computer this may be acceptable, but for multiple PCs
this could quickly become very tedious. Especially if you need to visit
the computer to make these changes.
Enter WindowsPowerShell
Windows
PowerShell is quite the helpful administrator’s tool. There are
commands for a great many Windows functions without leaving the local
machine. Once other tools are added, like Active Directory and Exchange,
the capabilities of PowerShell go through the ceiling. In this case, we
want to rename a computer using PowerShell. The way to do this using
the rename-computer cmdlet is as follows:
Rename-computer –computername “computer” –newname “newcomputername” –
domaincredential domain\user –force –restart
Running this cmdlet and supplying the current computer name for –computername and the new computer name for –newname along with a user account that has permission to perform the function in Active Directory for the –domaincredential parameter. Supplying the –force
parameter ensures that the changes will be applied and –restart will
force the computer to restart after the change is made, still a
requirement for renaming computers.
This may seem like the same
amount of effort as using the screens to change the name, but I assure
you, even for one machine, it is faster. When I used this recently, I
didn’t need to logon to complete the action and as soon as the command
completed, the computer was rebooting. Since the client I renamed was
not sitting in front of me, by the time I walked across my office to see
if it was going to get renamed, the system was already shutting down as
directed by the –restart switch.
Where this gets really
handy is if you need to rename several computers. The cmdlet doesn’t
really change all that much either, with the biggest difference being
how the –computername and –newname parameters are handled. For example,
to get this to work with multiple computers you might do something like
this:
$computers = get-content “c:\computers-to-rename.txt”
$newnames = get-content “c:\newcomputernames.txt”
Foreach($computer in $computers)
{
Foreach ($newname in $newnames)
{
Rename-computer –computername $computer –newname $newname –domaincredential domain\user –force -restart
}
}
This script creates two variables containing lists of
computer names $computers and $newnames. Then it loops through each
$computer in the first list. As part of that loop, it also then loops
through each $newname in $newnames to get both $computer and $newname
for each item. The two text files assume one computer name per line and
no other information.
There are many other ways to tackle this
problem, although you will likely need lists or loops of some kind to
plow through the items. You might choose to populate your computers list
another way, maybe by getting the names of existing systems from Active
Directory:
$computers = Get-adcomputer | where {$_.name –like “sales*”}
This will get computer names from Active Directory where
the name starts with sales. Then you could loop through each of these
and the new names list and rename the computers.
If you want to use a new naming convention that begins with a name and contains a number that you can increment you might try:
$computers = Get-adcomputer | where {$_.name –like “sales-*”}
$num = 0
Foreach($computer in $computers)
{
For($num=1;$num –lt 5;$num++)
{
Rename-computer –computername $computer –newname “s-$num” –domaincredential domain\user –force –restart
}
}
Note: Appending the
–whatif switch on the Rename-computer cmdlet will display what the
command would do if it actually executed. This can give you an idea of
the performance without actually changing the computer name.
Since
the newname parameter is essentially incrementing the same name by one
for up to 50 computers this way will also allow a fairly quick renaming
of computers. In most cases, I would likely use the two foreach() loops
discussed above. Unless your environment rolls PCs over frequently, I
would imagine the single cmdlet would be of most use with the specified
computername and newname.
PowerShell is a very powerful and
helpful tool to add to any Windows administrators arsenal of day to day
tools. Using it to rename computers is much faster than accomplishing
the same task through the traditional method of using the control panel.
The more computers you need to rename, the more value you will see from
allowing PowerShell to do the lifting.
0 comments:
Post a Comment
Appreciate your concern ...