Get-ComputerName.ps1


posted by Thomas Lee
11-24-2008

Downloads: 634
File size: 1.8kB
Views: 12,790

Embed
Get-ComputerName.ps1
  1. # Get-ComputerName.ps1 
  2. # Gets system's computer name using PowerShell 
  3. # Sample 3 from http://msdn.microsoft.com/en-us/library/aa394586 
  4. # Thomas Lee - tfl@psp.co.uk 
  5.  
  6. # Get Computer info 
  7. $Computer = Get-WmiObject -Class Win32_ComputerSystem 
  8.  
  9. # Print Computer Name 
  10. "Computer Name is: {0}" -f $Computer.Name 
  11.  
  12.  
  13. # SIG # Begin signature block 
  14. # MIID6QYJKoZIhvcNAQcCoIID2jCCA9YCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB 
  15. # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR 
  16. # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUPHiwlwTR8mAwMvrVr5qR1zcZ 
  17. # +fegggIKMIICBjCCAXOgAwIBAgIQ6QrzeNOdlYNOBOf8UE5TTDAJBgUrDgMCHQUA 
  18. # MBUxEzARBgNVBAMTClRob21hcyBMZWUwHhcNMDgwODI5MTEyNjIyWhcNMzkxMjMx 
  19. # MjM1OTU5WjAVMRMwEQYDVQQDEwpUaG9tYXMgTGVlMIGfMA0GCSqGSIb3DQEBAQUA 
  20. # A4GNADCBiQKBgQDtfaM67/oYBpAPhkGIoxCOS8FzT+tSDps4kduxaDH2fJZLHNWP 
  21. # nDSsMaH1/XehVbhlTWJvsBFX9Un+7ItZQjhGXKbHNBacQCq6JlGF0rVpvvzRwoxr 
  22. # XlIMgzL8ktMokuD4IsRZcq3dM9aIXx39a4kZGqutDV5UAdbnCEMeQwLDAQIDAQAB 
  23. # o18wXTATBgNVHSUEDDAKBggrBgEFBQcDAzBGBgNVHQEEPzA9gBDYIQcIXk629sZL 
  24. # z8VaTRpKoRcwFTETMBEGA1UEAxMKVGhvbWFzIExlZYIQ6QrzeNOdlYNOBOf8UE5T 
  25. # TDAJBgUrDgMCHQUAA4GBAMCrIgPs4Ijlgpr2xTdGnwYtskeV32rjPdvQOpf63q6q 
  26. # 9Keh4vuz9S8iJW1Mho43eJQhE0llmp0kT4q/E9DcY8wo+KP6Giugr8gDKj1QpcuF 
  27. # Izp6E0l5HBIow1JnpD6n2mmn1i80Dm+YJLyHYjouPzvvCefszR2s6nf/qR4ByzHz 
  28. # MYIBSTCCAUUCAQEwKTAVMRMwEQYDVQQDEwpUaG9tYXMgTGVlAhDpCvN4052Vg04E 
  29. # 5/xQTlNMMAkGBSsOAwIaBQCgeDAYBgorBgEEAYI3AgEMMQowCKACgAChAoAAMBkG 
  30. # CSqGSIb3DQEJAzEMBgorBgEEAYI3AgEEMBwGCisGAQQBgjcCAQsxDjAMBgorBgEE 
  31. # AYI3AgEVMCMGCSqGSIb3DQEJBDEWBBRomv5yjUv60y5+rDHdPOcI+rnvvTANBgkq 
  32. # hkiG9w0BAQEFAASBgNnWu3uPZj0tAb1JPmxstNnmIxuxH73QE/Xwccl8qLuOFerY 
  33. # amzXMuxiZE3aUSBZXyv7Qhecx3lAoiKoLlaYjekz04ALlMqy9Zg50h95WUEGek2k 
  34. # rs9XtlfFewfS40sKL2AZ26qh781BKFW6dy6vkI9ztxEa3YZ/K4tCsaX6bqeM 
  35. # SIG # End signature block 

This script uses the Win32_ComputerSystem to get the current computer's computer name.

 

Comments

Tobias Weltner wrote re: Get-ComputerName.ps1
on 11-26-2008 9:26 AM

Another way might be:

$env:computername

However this only works for local machines of course while WMI can also contact remote machines using -computerName parameter

Copyright 2012 PowerShell.com. All rights reserved.