about_Type_Accelerators

about_Type_Accelerators

PowerShell에서 [System.Management.Automation.AliasAttribute]처럼 긴 .NET 형식 이름을 코드에 매번 그대로 적는 건 여간 번거로운 일이 아니에요. 그럴 때 [Alias]처럼 짧게 줄여 쓸 수 있게 해 주는 게 바로 **형식 단축 이름(type accelerator)**이에요. .NET 형식에 붙여 놓은 별명(alias)이라고 생각하면 돼요. 이 글은 PowerShell이 기본으로 제공하는 형식 단축 이름 목록과, 그걸 어디에 어떻게 쓰는지를 설명해요.

단축 이름은 대부분 소문자로 정의돼 있어요. 다만 일부는 Pascal-case(단어 첫 글자를 대문자로 쓰는 표기)로 정의된 것도 있죠. PowerShell은 대소문자를 구분하지 않으니까 둘 중 어떤 표기로 써도 똑같이 동작해요.

출처: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators

본문

형식 단축 이름 사용하기

대부분의 형식 단축 이름은 전체 형식 이름을 쓰는 것과 똑같은 방식으로 쓰면 돼요. 다만 PowerShell이 다음 두 단축 이름에 대해서만은 특별하게 처리해요.

형식 단축 이름은 주로 변수의 형식을 지정하거나 객체를 특정 형식으로 캐스트(cast)할 때 가장 많이 쓰여요. 그런 경우에는 형식 이름이나 단축 이름을 대괄호([])로 감싸 줘야 해요. 예를 들어 [int][int32]처럼 말이죠.

그런데 어떤 상황에서는 형식 단축 이름을 문자열로 지정할 수도 있어요. 예를 들어볼게요.

형식 비교 연산자와 함께 쓸 때

PS> '1' -as 'int'
1
PS> 1 -is 'int'
True

[type] 형식 클래스와 함께 쓸 때

PS> [type]'int'

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

이렇게 문자열로 쓸 수 있는 경우가 있는 반면에, reflection처럼 다른 맥락에서는 형식 단축 이름 대신 전체 형식 이름을 문자열로 써야 해요.

기본 형식 단축 이름

PowerShell 7.6이 기본으로 제공하는 형식 단축 이름 목록이에요. 왼쪽이 단축 이름, 오른쪽이 그에 대응하는 전체 .NET 형식 이름이에요.

단축 이름 전체 .NET 형식 이름
adsi System.DirectoryServices.DirectoryEntry
adsisearcher System.DirectoryServices.DirectorySearcher
Alias System.Management.Automation.AliasAttribute
AllowEmptyCollection System.Management.Automation.AllowEmptyCollectionAttribute
AllowEmptyString System.Management.Automation.AllowEmptyStringAttribute
AllowNull System.Management.Automation.AllowNullAttribute
ArgumentCompleter System.Management.Automation.ArgumentCompleterAttribute
ArgumentCompletions System.Management.Automation.ArgumentCompletionsAttribute
array System.Array
bigint System.Numerics.BigInteger
bool System.Boolean
byte System.Byte
char System.Char
cimclass Microsoft.Management.Infrastructure.CimClass
cimconverter Microsoft.Management.Infrastructure.CimConverter
ciminstance Microsoft.Management.Infrastructure.CimInstance
CimSession Microsoft.Management.Infrastructure.CimSession
cimtype Microsoft.Management.Infrastructure.CimType
CmdletBinding System.Management.Automation.CmdletBindingAttribute
cultureinfo System.Globalization.CultureInfo
datetime System.DateTime
decimal System.Decimal
double System.Double
DscLocalConfigurationManager System.Management.Automation.DscLocalConfigurationManagerAttribute
DscProperty System.Management.Automation.DscPropertyAttribute
DscResource System.Management.Automation.DscResourceAttribute
ExperimentAction System.Management.Automation.ExperimentAction
Experimental System.Management.Automation.ExperimentalAttribute
ExperimentalFeature System.Management.Automation.ExperimentalFeature
float System.Single
guid System.Guid
hashtable System.Collections.Hashtable
initialsessionstate System.Management.Automation.Runspaces.InitialSessionState
int System.Int32
int16 System.Int16
int32 System.Int32
int64 System.Int64
ipaddress System.Net.IPAddress
IPEndpoint System.Net.IPEndPoint
long System.Int64
mailaddress System.Net.Mail.MailAddress
NoRunspaceAffinity System.Management.Automation.Language.NoRunspaceAffinityAttribute
NullString System.Management.Automation.Language.NullString
ObjectSecurity System.Security.AccessControl.ObjectSecurity
ordered System.Collections.Specialized.OrderedDictionary
OutputType System.Management.Automation.OutputTypeAttribute
Parameter System.Management.Automation.ParameterAttribute
PhysicalAddress System.Net.NetworkInformation.PhysicalAddress
powershell System.Management.Automation.PowerShell
psaliasproperty System.Management.Automation.PSAliasProperty
pscredential System.Management.Automation.PSCredential
pscustomobject System.Management.Automation.PSObject
PSDefaultValue System.Management.Automation.PSDefaultValueAttribute
pslistmodifier System.Management.Automation.PSListModifier
psmoduleinfo System.Management.Automation.PSModuleInfo
psnoteproperty System.Management.Automation.PSNoteProperty
psobject System.Management.Automation.PSObject
psprimitivedictionary System.Management.Automation.PSPrimitiveDictionary
pspropertyexpression Microsoft.PowerShell.Commands.PSPropertyExpression
psscriptmethod System.Management.Automation.PSScriptMethod
psscriptproperty System.Management.Automation.PSScriptProperty
PSTypeNameAttribute System.Management.Automation.PSTypeNameAttribute
psvariable System.Management.Automation.PSVariable
psvariableproperty System.Management.Automation.PSVariableProperty
ref System.Management.Automation.PSReference
regex System.Text.RegularExpressions.Regex
runspace System.Management.Automation.Runspaces.Runspace
runspacefactory System.Management.Automation.Runspaces.RunspaceFactory
sbyte System.SByte
scriptblock System.Management.Automation.ScriptBlock
securestring System.Security.SecureString
semver System.Management.Automation.SemanticVersion
short System.Int16
single System.Single
string System.String
SupportsWildcards System.Management.Automation.SupportsWildcardsAttribute
switch System.Management.Automation.SwitchParameter
timespan System.TimeSpan
type System.Type
uint System.UInt32
uint16 System.UInt16
uint32 System.UInt32
uint64 System.UInt64
ulong System.UInt64
uri System.Uri
ushort System.UInt16
ValidateCount System.Management.Automation.ValidateCountAttribute
ValidateDrive System.Management.Automation.ValidateDriveAttribute
ValidateLength System.Management.Automation.ValidateLengthAttribute
ValidateNotNull System.Management.Automation.ValidateNotNullAttribute
ValidateNotNullOrEmpty System.Management.Automation.ValidateNotNullOrEmptyAttribute
ValidateNotNullOrWhiteSpace System.Management.Automation.ValidateNotNullOrWhiteSpaceAttribute
ValidatePattern System.Management.Automation.ValidatePatternAttribute
ValidateRange System.Management.Automation.ValidateRangeAttribute
ValidateScript System.Management.Automation.ValidateScriptAttribute
ValidateSet System.Management.Automation.ValidateSetAttribute
ValidateTrustedData System.Management.Automation.ValidateTrustedDataAttribute
ValidateUserDrive System.Management.Automation.ValidateUserDriveAttribute
version System.Version
void System.Void
WildcardPattern System.Management.Automation.WildcardPattern
wmi System.Management.ManagementObject
wmiclass System.Management.ManagementClass
wmisearcher System.Management.ManagementObjectSearcher
X500DistinguishedName System.Security.Cryptography.X509Certificates.X500DistinguishedName
X509Certificate System.Security.Cryptography.X509Certificates.X509Certificate
xml System.Xml.XmlDocument

더 알아보기