0

I am trying to rename Windows (10) PC using a PowerShell command in a generic script. Here is what I have so far, but it doesn't work:

Rename-Computer -NewName "New-Name-Based-On-Script-Parent-Folder" -DomainCredential [System.Security.Principal.WindowsIdentity]::GetCurrent().Name -Restart

I have tried running this in a PowerShell instance with administrator privileges, as well as an administrative instance of PowerShell ISE (after running Set-ExecutionPolicy Bypass -Scope Process of course), but to no avail.

Ideally, I would like this to run on any Windows PC and rename it according to the name of the parent folder that the script resides in. Any suggestions on what I'm doing wrong? Thanks.

ACE
  • 1
  • 1
  • What error are you getting when you run that? – TheMadTechnician Feb 03 '21 at 18:35
  • 1
    Your question is unclear as to what your specific issue is, so based upon `"New-Name-Based-On-Script-Parent-Folder"`, I'd suggest you look [here](https://stackoverflow.com/q/5466329). – Compo Feb 03 '21 at 18:51

1 Answers1

0

If you're just wanting to name the computer after the folder you're in, you could try something like this:

## Get current folder
$folderName = Get-Location
## Drop the drive letter
$folder = Split-Path -Path $folderName -Leaf 
## Display folder name
$folder

Rename-Computer -ComputerName $folder -DomainCredential "domain\$env:USERNAME"

If you want more to the name, you could add to the new computer name.

ZebulaCodes
  • 13
  • 1
  • 6
  • Thank you for the proposed solution. However, upon running this script, I get the following error in PowerShell: `code` Split-Path : Cannot bind argument to parameter 'Path' because it is null. At C:\PCName\Rename computer Powershell script.ps1:20 char:29 + $folder = Split-Path -Path $folder -Leaf + ~~~~~~~ + CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand `code` – ACE Feb 03 '21 at 22:50
  • I may have messed up. I don't get that error with either one of these, but try: ``` $folderName = Get-Location $folder = Split-Path -Path $folderName -Leaf $folder Rename-Computer -ComputerName $folder -DomainCredential "domain\$env:USERNAME" ``` – ZebulaCodes Feb 03 '21 at 22:58