about_Properties
업데이트 날짜: 2012년 8월
적용 대상: Windows PowerShell 2.0, Windows PowerShell 3.0
항목
about_Properties
간단한 설명
Windows PowerShell에서 개체 속성을 사용하는 방법을 설명합니다.
자세한 설명
Windows PowerShell에서는 개체라는 구조적 정보 컬렉션을 사용하여 데이터 저장소의 항목이나 컴퓨터 상태를 나타냅니다. 일반적으로 Microsoft .NET Framework 일부인 개체를 사용하지만 Windows PowerShell에서 사용자 지정 개체를 만들 수도 있습니다.
항목과 해당 개체 간의 연결은 매우 가깝습니다. 개체를 변경할 때 일반적으로 개체가 나타내는 항목을 변경합니다. 예를 들어 Windows PowerShell에서 파일을 가져올 때 실제 파일을 가져오는 것이 아니라 파일을 나타내는 FileInfo 개체를 가져옵니다. FileInfo 개체를 변경하면 파일도 변경됩니다.
대부분 개체에는 속성이 있습니다. 속성은 개체와 연결된 데이터입니다. 개체 형식에 따라 속성도 다릅니다. 예를 들어 파일을 나타내는 FileInfo 개체에는 파일에 read-only 특성이 있으면 $True가 포함되고 없으면 $False가 포함되는 IsReadOnly 속성이 있습니다. 파일 시스템 디렉터리를 나타내는 DirectoryInfo 개체에는 부모 디렉터리의 경로가 포함된 Parent 속성이 있습니다.
개체 속성
개체 속성을 가져오려면 Get-Member cmdlet을 사용합니다. 예를 들어 FileInfo 개체의 속성을 가져오려면 Get-ChildItem cmdlet을 사용하여 파일을 나타내는 FileInfo 개체를 가져옵니다. 그다음에 파이프라인 연산자(|)를 사용하여 FileInfo 개체를 Get-Member로 보냅니다. 다음 명령은 PowerShell.exe 파일을 가져와서 Get-Member로 보냅니다. $Pshome 자동 변수에는 Windows PowerShell 설치 디렉터리의 경로가 포함됩니다.
Get-ChildItem $pshome\PowerShell.exe | Get-Member
명령 출력에는 FileInfo 개체의 멤버가 나열됩니다. 멤버에는 속성 및 메서드가 둘 다 포함됩니다. Windows PowerShell에서 작업할 때 개체의 모든 멤버에 액세스할 수 있습니다.
메서드를 제외하고 개체의 속성만 가져오려면 다음 예제와 같이 Get-Member cmdlet의 MemberType 매개 변수를 "property" 값과 함께 사용합니다.
Get-ChildItem $pshome\PowerShell.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;}
속성을 찾고 나서 Windows PowerShell 명령에서 사용할 수 있습니다.
속성 값
특정 형식의 모든 개체에는 같은 속성이 있지만 해당 속성의 값은 특정 개체를 설명합니다. 예를 들어 모든 FileInfo 개체에는 CreationTime 속성이 있지만 해당 속성의 값은 파일별로 다릅니다.
개체의 속성 값을 가져오는 가장 일반적인 방법은 점 방법을 사용하는 것입니다. 개체가 포함된 변수와 같은 개체에 대한 참조를 입력하거나 개체를 가져오는 명령을 입력합니다. 그다음에 점(.) 뒤에 속성 이름을 입력합니다.
예를 들어 다음 명령은 PowerShell.exe 파일의 CreationTime 속성 값을 표시합니다. Get-ChildItem 명령은 PowerShell.exe 파일을 나타내는 FileInfo 개체를 반환합니다. 속성에 액세스하기 전에 명령이 실행되도록 명령을 괄호로 묶습니다. 다음과 같이 Get-ChildItem 명령 뒤에 점 및 CreationTime 속성을 입력합니다.
C:\PS> (Get-ChildItem $pshome\PowerShell.exe).creationtime
Tuesday, March 18, 2008 12:07:52 AM
다음 예제와 같이 개체를 변수에 저장하고 점 방법을 사용하여 속성을 가져올 수도 있습니다.
C:\PS> $a = Get-ChildItem $pshome\PowerShell.exe
C:\PS> $a.CreationTime
Tuesday, March 18, 2008 12:07:52 AM
Select-Object 및 Format-List cmdlet을 사용하여 개체의 속성 값을 표시할 수도 있습니다. Select-Object 및 Format-List에는 각각 Property 매개 변수가 있습니다. Property 매개 변수를 사용하여 하나 이상의 속성과 해당 값을 지정할 수 있습니다. 또는 와일드카드 문자(*)를 사용하여 모든 속성을 나타낼 수 있습니다.
예를 들어 다음 명령은 PowerShell.exe 파일의 모든 속성 값을 표시합니다.
C:\PS> Get-ChildItem $pshome\PowerShell.exe | Format-List -property *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32\WindowsPowerShell\v1.0
PSChildName : PowerShell.exe
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
VersionInfo : File: C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe
InternalName: POWERSHELL
OriginalFilename: PowerShell.EXE.MUI
File Version: 6.1.6570.1 (fbl_srv_PowerShell(nigels).070711-0102)
FileDescription: PowerShell.EXE
Product: Microsoft® Windows® Operating System
ProductVersion: 6.1.6570.1
Debug: False
Patched: False
PreRelease: False
PrivateBuild: True
SpecialBuild: False
Language: English (United States)
BaseName : PowerShell
Mode : -a---
Name : PowerShell.exe
Length : 160256
DirectoryName : C:\Windows\system32\WindowsPowerShell\v1.0
Directory : C:\Windows\system32\WindowsPowerShell\v1.0
IsReadOnly : False
Exists : True
FullName : C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe
Extension : .exe
CreationTime : 3/18/2008 12:07:52 AM
CreationTimeUtc : 3/18/2008 7:07:52 AM
LastAccessTime : 3/19/2008 8:13:58 AM
LastAccessTimeUtc : 3/19/2008 3:13:58 PM
LastWriteTime : 3/18/2008 12:07:52 AM
LastWriteTimeUtc : 3/18/2008 7:07:52 AM
Attributes : Archive
정적 속성
Windows PowerShell에서 NET 클래스의 정적 속성을 사용할 수 있습니다. 정적 속성은 개체 속성인 표준 속성과 달리 클래스 속성입니다.
클래스의 정적 속성을 가져오려면 Get-Member cmdlet의 Static 매개 변수를 사용합니다.
예를 들어 다음 명령은 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 클래스의 UtcNow 정적 속성 값을 가져옵니다.
[System.DateTime]::UtcNow
스칼라 개체 및 컬렉션 속성
특정 형식 단일("스칼라") 개체의 속성은 같은 형식 개체 컬렉션의 속성과 다릅니다.
예를 들어 모든 서비스에는 DisplayName 속성이 있지만 서비스 컬렉션에는 DisplayName 속성이 없습니다. 마찬가지로 모든 컬렉션에는 컬렉션의 개체 수를 알리는 Count 속성이 있지만 개별 개체에는 Count 속성이 없습니다.
Windows PowerShell 3.0부터 Windows PowerShell에서는 스칼라 개체 및 컬렉션의 속성 차이로 인해 발생하는 스크립팅 오류를 방지하려고 합니다.
-- 컬렉션을 전송하지만 단일("스칼라") 개체에만 있는 속성을 요청하면 Windows PowerShell에서는 컬렉션에 있는 모든 개체에 대한 해당 속성 값을 반환합니다.
-- 개체가 없는 경우나 단일 개체의 Count 또는 Length 속성을 요청하면 Windows PowerShell에서는 올바른 값을 반환합니다.
속성이 개별 개체 및 컬렉션에 있으면 Windows PowerShell은 결과를 변경하지 않습니다.
이 기능은 스칼라 개체 및 컬렉션의 메서드에서도 작동합니다. 자세한 내용은 about_Methods를 참조하세요.
예제
예를 들어 각 서비스에는 DisplayName 속성이 있습니다. 다음 명령은 Audiosrv 서비스의 DisplayName 속성 값을 가져옵니다.
PS C:\>(Get-Service Audiosrv).DisplayName
Windows Audio
그러나 서비스의 컬렉션 또는 배열에는 DisplayName이 없습니다. 다음 명령은 Windows PowerShell 2.0에서 모든 서비스의 DisplayName 속성을 가져오려고 합니다.
PS C:\>(Get-Service).DisplayName
PS C:\>
Windows PowerShell 3.0부터 같은 명령은 Get-Service가 반환하는 모든 서비스의 DisplayName 속성 값을 반환합니다.
PS C:\>(Get-Service).DisplayName
Application Experience
Application Layer Gateway Service
Windows All-User Install Agent
Application Identity
Application Information
...
반대로, 둘 이상의 서비스가 포함된 컬렉션에는 컬렉션의 개체 수가 들어 있는 Count 속성이 있습니다.
PS C:\>(Get-Service).Count
176
Windows PowerShell 2.0의 이 명령에서 확인한 것처럼 개별 서비스에는 Count 또는 Length 속성이 없습니다.
PS C:\>(Get-Service Audiosrv).Count
PS C:\>
Windows PowerShell 3.0부터 이 명령은 올바른 Count 값을 반환합니다.
PS C:\>(Get-Service Audiosrv).Count
1
참고 항목
about_methods
about_Objects
Get-Member
Select-Object
Format-List