Get‑ComputerName - dsccommunity/DscResource.Common GitHub Wiki

SYNOPSIS

Returns the computer name cross-plattform.

SYNTAX

Get-ComputerName [-FullyQualifiedDomainName] [<CommonParameters>]

DESCRIPTION

Returns the computer name cross-plattform. The variable $env:COMPUTERNAME does not exist cross-platform which hinders development and testing on macOS and Linux. Instead this command can be used to get the computer name cross-plattform.

EXAMPLES

EXAMPLE 1

Get-ComputerName

Returns the computer name regardless of platform.

EXAMPLE 2

Get-ComputerName -FullyQualifiedDomainName

Returns the fully qualified domain name regardless of platform.

PARAMETERS

-FullyQualifiedDomainName

Returns the fully qualified domain name (FQDN) instead of just the computer name.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

System.String

NOTES

The function uses [System.Environment]::MachineName for the short computer name, which works consistently across all platforms where PowerShell runs.

When the FullyQualifiedDomainName switch is used, the function attempts to retrieve the FQDN using [System.Net.Dns]::GetHostByName() which can resolve the full domain name when the system is properly configured with DNS.

If DNS resolution fails or no domain is configured, the function will fall back to returning the short computer name even when FQDN is requested.

RELATED LINKS