EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell ComputerName

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell ComputerName

PowerShell ComputerName

Introduction to PowerShell ComputerName

It is always useful and handy to know the computer name or the local host of the system you are logged into. It is also needed to run certain cmdlets as they require computer name as a mandatory parameter. It is also very important while working with remote computers or when some script needs to be run on remote computers. This article will cover various ways in which the computer name can find out using PowerShell.

Ways of Finding the Computer Name

There are many ways and commands in which the Computer Name can be identified. The following are some of the ways.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • From the Environment variable
  • Using hostname process way
  • Using WMI/CIM
  • Using .Net machine value
  • From Kernel function
  • Using Gethostname() function of .Net

We will see each of the above in detail.

1. Finding Computer Name from the Environment Variable

The easiest way of finding out the computer name is by reading the environment variable. This is the simplest, most effective, and fastest way. It also suffers from a major drawback, since the environment variable can be easily overwritten it comes with a bit of risk factor. The result may be skewed.

Input:

$env:computername

Output:

PowerShell ComputerName-1.1

To get all the environment variables the following cmdlet can be used.

Get-Childitem env:

To get the computer name the following cmdlet can be used.

Input:

get-childitem env:computername

Output:

PowerShell ComputerName-1.2

2. Using Host Name Process Way

Like linux, windows also have some binaries which can be used to fetch information. Executing the hostname.exe will return the computer name. Running the below command will display the computer name

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,751 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

Input:

hostname.exe

Output:

PowerShell ComputerName-1.3

3. Using the WMI

Wmi is available on windows machines since the windows 2000 version. In the versions prior to it, it was an external package. The same can be achieved using the CIM Instance.

Input:

Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
(Get-CIMInstance CIM_ComputerSystem).Name

Output:

Output-1.4

4. Using .Net Class

The computer name can be found using the .Net class.

Input:

[system.environment]::MachineName
Similarly using the gethostname function the computer name can be found out.
[System.Net.Dns]::GetHostName()

Output:

Output-1.5

5. Renaming a Computer Name

It is possible that sometimes an administrator needs to rename the computer name. In that case, the Rename-Computer cmdlet can be used.

Syntax:

NAME
Rename-Computer
SYNTAX
Rename-Computer [-NewName] <string> [-ComputerName <string>] [-PassThru] [-DomainCredential <pscredential>] [-LocalCredential <pscredential>] [-Force] [-Restart] [-WsmanAuthentication {Default | Basic | Negotiate | CredSSP | Digest | Kerberos}] [-Protocol {DCOM | WSMan}] [-WhatIf] [-Confirm]  [<CommonParameters>] ALIASES
None

Parameters
  • -ComputerName: This denotes the computer to be renamed. It can be either the local system or a remote computer. By default, it refers to the local system. To rename the local computer, mention as localhost or. To rename the remote computer, specify the ip address or netbios name or the fqdn name of the remote system. Its type is a string. It accepts pipeline input, but wild card characters are not supported.
  • -Confirm: This denotes that a confirmation message must be shown before the cmdlet is executed. The data type is a switch and can also be accessed using cf. Pipeline input is not accepted, and wild card characters are not allowed.
  • -DomainCredential: This denotes the account with which to connect to the domain. If a system is joined to a domain, then credentials are mandatory. Its type is PSCredential and the default value is none. Pipeline input is not accepted, and wild card characters are not allowed.
  • -Force: This denotes the cmdlet to be run without user confirmation.
  • -Local Credential: This denotes the account which has permission to the computer. By default, current user credentials are taken. If the prompt for credentials shouldn’t appear domain credential parameter should be used. Its type is PSCredential. It doesn’t accept pipeline input and wild card characters are not allowed.
  • -NewName: This is the new name of the computer. Spaces and periods are not allowed. The maximum number of characters allowed is 63 and it can’t contain only numbers. It is of data type string. Pipeline input is accepted.
  • -PassThru: This is used to show the preview of the command if run. It will not generate any output. The data type of this parameter is a switch. By default, the value is false.
  • -Restart: This denotes for the system to be restarted after the name change. Mostly a restart is required for the name change to be effective. Its type is a switch. Its default value is none. It doesn’t accept pipeline input and wild card characters are also not allowed.
  • -Whatif: This will not result in any output. It is like a preview. The data type of the parameter is a switch and can be accessed as wi. If the value is not supplied, it is considered false.
  • -WsmanAuthentication: It denotes the mechanism of the WSman protocol that will be used to authenticate the credentials. The possible values are basic, Credssp, Default, Digest, Kerberos, and negotiate. Its type is a string. The default value is none. It doesn’t accept pipeline input and wild card characters are also not allowed.

Example of PowerShell ComputerName

Example of PowerShell computername is given below:

Input:

Write-Host "Welcome to renaming of computer name demo"
Write-Host "Renaming of local computer"
Rename-Computer -NewName "vigneshnew" -DomainCredential home\vignesh
Write-Host "Computer renamed successfully"
Write-Host "Renaming of remote computer"
Rename-Computer -ComputerName "testserver1" -NewName "testservernew" -DomainCredential test\vignesh -Force
Write-Host "Computer renamed successfully"

Output:

Output-1.6

Conclusion

Thus, the article covered in detail identifying the name of a local computer or a remote computer name in PowerShell. It also explained the various ways in which it can be obtained using examples. The article also covered in detail the ways in which a computer name can be changed. To learn more in detail it is advisable to write sample scripts and practice them.

Recommended Articles

This is a guide to PowerShell ComputerName. Here we also discuss the introduction and ways of finding the computer name along with an example and its code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell Registry
  2. PowerShell Sleep
  3. Windows PowerShell ISE
  4. PowerShell Rename Folder

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Shell Scripting Course Learn More