간단한 설명
PowerShell에서 개체 속성을 사용하는 방법을 설명합니다.
긴 설명
PowerShell은 개체라는 구조적 정보 컬렉션을 사용하여 데이터 저장소의 항목 또는 컴퓨터 상태를 나타냅니다. 일반적으로 Microsoft .NET Framework의 일부인 개체를 사용하지만 PowerShell에서 사용자 지정 개체를 만들 수도 있습니다.
항목과 해당 개체 간의 연결이 매우 가깝습니다. 개체를 변경할 때는 일반적으로 개체가 나타내는 항목을 변경합니다. 예를 들어 PowerShell에서 파일을 가져올 때 실제 파일은 가져올 수 없습니다. 대신 파일을 나타내는 FileInfo 개체를 가져옵니다. FileInfo 개체를 변경하면 파일도 변경됩니다.
대부분의 개체에는 속성이 있습니다. 속성은 개체와 연결된 데이터입니다. 개체 유형에 따라 속성이 다릅니다. 예를 들어 파일을 나타내는 FileInfo 개체에는 파일에 읽기 전용 특성이 있고 그렇지 않은 경우 경우 $True 포함하는 $False 속성이 있습니다. 파일 시스템 디렉터리를 나타내는 DirectoryInfo 개체에는 부모 디렉터리의 경로가 포함된 Parent 속성이 있습니다.
개체 속성
개체의 속성을 얻으려면 Get-Member cmdlet을 사용합니다. 예를 들어 FileInfo 개체의 속성을 얻으려면 Get-ChildItem cmdlet을 사용하여 파일을 나타내는 FileInfo 개체를 가져옵니다. 그런 다음 파이프라인 연산자(|)를 사용하여 FileInfo 개체를 보내 Get-Member. 다음 명령은 pwsh.exe 파일을 가져와서 Get-Member보냅니다.
$PSHOME 자동 변수에는 PowerShell 설치 디렉터리의 경로가 포함됩니다.
Get-ChildItem $PSHOME\pwsh.exe | Get-Member
명령의 출력에는 FileInfo 개체의 멤버가 나열됩니다. 멤버에는 속성과 메서드가 모두 포함됩니다. PowerShell에서 작업하는 경우 개체의 모든 멤버에 액세스할 수 있습니다.
메서드가 아닌 개체의 속성만 얻으려면 다음 예제와 같이 cmdlet의 Get-Member 매개 변수를 Property값으로 사용합니다.
Get-ChildItem $PSHOME\pwsh.exe | Get-Member -MemberType Property
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property System.DateTime CreationTime {get;set;}
CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property System.String DirectoryName {get;}
Exists Property System.Boolean Exists {get;}
Extension Property System.String Extension {get;}
FullName Property System.String FullName {get;}
IsReadOnly Property System.Boolean IsReadOnly {get;set;}
LastAccessTime Property System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime Property System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;}
Length Property System.Int64 Length {get;}
Name Property System.String Name {get;}
속성을 찾은 후 PowerShell 명령에서 사용할 수 있습니다.
속성 값
특정 형식의 모든 개체에는 동일한 속성이 있지만 해당 속성의 값은 특정 개체를 설명합니다. 예를 들어 모든 FileInfo 개체에는 CreationTime 속성이 있지만 해당 속성의 값은 각 파일에 대해 다릅니다.
개체의 속성 값을 가져오는 가장 일반적인 방법은 멤버 액세스 연산자(.)를 사용하는 것입니다. 개체를 포함하는 변수 또는 개체를 가져오는 명령과 같은 개체에 대한 참조를 입력합니다. 그런 다음 연산자(.)와 속성 이름을 입력합니다.
예를 들어 다음 명령은 파일의 pwsh.exe 속성 값을 표시합니다.
Get-ChildItem 명령은 나타내는 pwsh.exe file 개체를 반환합니다. 이 명령은 속성에 액세스하기 전에 실행되도록 괄호로 묶습니다.
(Get-ChildItem $PSHOME\pwsh.exe).CreationTime
Tuesday, June 14, 2022 5:17:14 PM
다음 예제와 같이 변수에 개체를 저장한 다음 멤버 액세스(.) 메서드를 사용하여 해당 속성을 가져올 수도 있습니다.
$a = Get-ChildItem $PSHOME\pwsh.exe
$a.CreationTime
Tuesday, June 14, 2022 5:17:14 PM
Select-Object 및 Format-List cmdlet을 사용하여 개체의 속성 값을 표시할 수도 있습니다.
Select-Object 및 Format-List 각각에는 속성 매개 변수가 있습니다.
속성 매개 변수를 사용하여 하나 이상의 속성과 해당 값을 지정할 수 있습니다. 또는 와일드카드 문자(*)를 사용하여 모든 속성을 나타낼 수 있습니다.
예를 들어 다음 명령은 pwsh.exe 파일의 모든 속성 값을 표시합니다.
Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7\pwsh.exe
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7
PSChildName : pwsh.exe
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
Mode : -a---
ModeWithoutHardLink : -a---
VersionInfo : File: C:\Program Files\PowerShell\7\pwsh.exe
InternalName: pwsh.dll
OriginalFilename: pwsh.dll
FileVersion: 7.3.9.500
FileDescription: pwsh
Product: PowerShell
ProductVersion: 7.3.9 SHA: 116d193ed28dcc6914653c799846bbf379cea0fb
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: Language Neutral
BaseName : pwsh
ResolvedTarget : C:\Program Files\PowerShell\7\pwsh.exe
Target :
LinkType :
Length : 293312
DirectoryName : C:\Program Files\PowerShell\7
Directory : C:\Program Files\PowerShell\7
IsReadOnly : False
FullName : C:\Program Files\PowerShell\7\pwsh.exe
Extension : .exe
Name : pwsh.exe
Exists : True
CreationTime : 10/25/2023 7:00:18 PM
CreationTimeUtc : 10/26/2023 12:00:18 AM
LastAccessTime : 11/14/2023 5:14:36 PM
LastAccessTimeUtc : 11/14/2023 11:14:36 PM
LastWriteTime : 10/25/2023 7:00:18 PM
LastWriteTimeUtc : 10/26/2023 12:00:18 AM
LinkTarget :
UnixFileMode : -1
Attributes : Archive
정적 속성
PowerShell에서 .NET 클래스의 정적 속성을 사용할 수 있습니다. 정적 속성은 개체의 속성인 표준 속성과 달리 클래스의 속성입니다.
클래스의 정적 속성을 얻으려면 cmdlet의 Get-Member 매개 변수를 사용합니다. 예를 들어 다음 명령은 System.DateTime 클래스의 정적 속성을 가져옵니다.
Get-Date | Get-Member -MemberType Property -Static
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
MaxValue Property static datetime MaxValue {get;}
MinValue Property static datetime MinValue {get;}
Now Property datetime Now {get;}
Today Property datetime Today {get;}
UtcNow Property datetime UtcNow {get;}
정적 속성의 값을 얻으려면 다음 구문을 사용합니다.
[<ClassName>]::<Property>
예를 들어 다음 명령은 클래스의 System.DateTime 정적 속성 값을 가져옵니다.
[System.DateTime]::UtcNow
멤버 접근 권한 나열
PowerShell 3.0부터 멤버 액세스 연산자.()를 사용하여 목록 컬렉션에 없는 속성에 액세스할 때 PowerShell은 컬렉션의 항목을 자동으로 열거하고 각 항목의 속성 값을 반환합니다. 자세한 내용은 about_Member-Access_Enumeration참조하십시오.
예시
이 명령은 반환하는 모든 서비스의 Get-Service 속성 값을 반환합니다.
(Get-Service).DisplayName
Application Experience
Application Layer Gateway Service
Windows All-User Install Agent
Application Identity
Application Information
...
모든 컬렉션에는 컬렉션의 개체 수를 반환하는 Count 속성이 있습니다.
(Get-Service).Count
176
PowerShell 3.0부터 컬렉션이 아닌 singleton 개체의 Count 또는 Length 속성을 가져올 수 있습니다.
(Get-Service Audiosrv).Count
1
그러나 일부 개체에는 Length 속성이 있습니다. 예를 들어, 문자열의 길이 는 문자열에 있는 문자 수를 의미합니다. Count 속성은 개체의 인스턴스 수입니다.
PS> $str = 'string'
PS> $str.Length
6
PS> $str.Count
1
개별 개체 및 컬렉션에 속성이 있는 경우 컬렉션의 속성만 반환됩니다.
$collection = @(
[pscustomobject]@{length = "foo"}
[pscustomobject]@{length = "bar"}
)
# PowerShell returns the collection's Length.
$collection.length
2
참고하십시오
- 객체_정보
- 멤버-접근_열거_정보
- 방법에_관하여
- 형식-목록
- Get-멤버
- Select-Object