Get List of PC and Change Name PowerShell script

hello, i want Powershell script that give me list of all the computers in AD with the option of select one of them by number and after it i can change the computer name that i selected.

exsmple:
Get-ADComputer -Filter * -Property * | Select-Object CN
1. WS1
2. WS2
3. WS3
4. WS4
Select a PC: 2

Write-Host "Please enter your new computer name: "
$newname = Read-Host
## netdom renamecomputer $oldname /newname:$newname /userd:* /passwordd:* /usero:* /passwordo:* /reboot:10 /force
Rename-Computer -ComputerName "$oldname" -NewName "$newname" -DomainCredential amal-p\admin -Force -PassThru -Restart
Netanel ElhadadIT System AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I would use Out-GridView for the PC selection.
$pc = Get-ADComputer -Filter * | Select-Object CN | 
                      Out-GridView -OutputMode Single -Title 'Select one PC to rename'
$newname = Read-Host 'Please enter your new computer name: '
Rename-Computer -ComputerName $pc.CN -NewName $newname -DomainCredential amal-p\admin -Force -PassThru -Restart

Open in new window

Please do not enclose string variables in double quotes, unless you are concatening. As I showed, just use them without.
0
Netanel ElhadadIT System AdministratorAuthor Commented:
i add the " -Property * " and then its work perfect.

thanks a lot!!!

$pc = Get-ADComputer -Filter * -Property * | Select-Object CN |
                      Out-GridView -OutputMode Single -Title 'Select one PC to rename'
$newname = Read-Host 'Please enter your new computer name: '
Rename-Computer -ComputerName $pc.CN -NewName $newname -DomainCredential amal-p\admin -Force -PassThru -Restart
0
Michael B. SmithExchange & Active Directory ExpertCommented:
Just an efficiency comment -- you don't need "-Property *", you just need "-Property CN".

Retrieving one property instead of all properties should be noticeably faster and is certainly more efficient/performant.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry, thought the CN attribute is retrieved by default.
Yes, please only add that single property, not "*" - in AD that can make a lot of difference for performance and resources, as there might be some methods called for extended attributes internally.
Get-ADComputer -Filter * -Property * | select -First 1 | gm is fine to check for available properties interactively, but aside from that, never use -property *, and try to avoid -filter * if you can apply a filter. E.g. a good filter would be -Filter 'Enabled -eq $true' to skip disabled computer accounts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.