Skip to content

Class and Objects

Terminal window
class Person {
[string] $name = "Pavan"
[int16] $age = 20
[string] Print() {
return $this.name + " " + $this.age
}
}
$p1 = New-Object Person ; # Object creation
$p1.name = "Ravi"
$p1.Print() # Ravi 20
Remove-Object $p1 # this will delete the object..
$p1.Print(); # Error
Terminal window
class Person {
[string] $name;
[int16] $age;
Person() {
$this.name = "Pavan";
$this.age = 20;
}
Person([string] $name, [int] $age) {
$this.name = $name;
$this.age = $age;
}
[string] Print() {
return "Name: " + $this.name + ", Age: " + $this.age;
}
}
$p1 = New-Object Person("Arnav", 30);
$p2 = New-Object Person;
$p1.Print();
$p2.Print();
$p1.GetType(); # TYPE OF OBJECT
$p1.GetHashCode(); # ADDRESS
#OUTPUT
PS C:\Users\PavanKumarBandaru> C:\Users\PavanKumarBandaru\Desktop\Untitled1.ps1
Name: Arnav, Age: 30
Name: Pavan, Age: 20
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Person System.Object
62205972
Terminal window
Get-Member -InputObject $p1 ;
Terminal window
TypeName : Person
Name : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)
TypeName : Person
Name : GetHashCode
MemberType : Method
Definition : int GetHashCode()
TypeName : Person
Name : GetType
MemberType : Method
Definition : type GetType()
TypeName : Person
Name : Print
MemberType : Method
Definition : string Print()
TypeName : Person
Name : ToString
MemberType : Method
Definition : string ToString()
TypeName : Person
Name : age
MemberType : Property
Definition : int16 age {get;set;}
TypeName : Person
Name : name
MemberType : Property
Definition : string name {get;set;}
Terminal window
PS C:\Users\PavanKumarBandaru> **Get-Member -InputObject Get-Process**
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), i...
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinationI...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.S...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool E...
GetEnumerator Method System.CharEnumerator GetEnumerator(), System.Collections.IEnumer...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTy...
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex),...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int st...
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationF...
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int star...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationFor...
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, ch...
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, ...
Remove Method string Remove(int startIndex, int count), string Remove(int start...
Replace Method string Replace(char oldChar, char newChar), string Replace(string...
Split Method string[] Split(Params char[] separator), string[] Split(char[] se...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, Syst...
Substring Method string Substring(int startIndex), string Substring(int startIndex...
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower Method string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider)
ToString Method string ToString(), string ToString(System.IFormatProvider provide...
ToType Method System.Object IConvertible.ToType(type conversionType, System.IFo...
ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property int Length {get;}
Terminal window
PS C:\Users\PavanKumarBandaru> **Get-Process | Sort Id | Select-Object -First 20**
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
0 0 60 8 0 0 Idle
6622 0 44 128 4 0 System
0 0 176 111512 212 0 Secure System
0 21 14352 55324 256 0 Registry
1445 67 94608 15016 7.36 656 2 ms-teams
58 4 1272 1712 772 0 smss
186 41 12104 21220 976 0 svchost
1015 36 2536 8056 1108 0 csrss
167 12 1688 9980 1212 0 wininit
340 39 12200 30492 1.42 1264 2 taskhostw
393 17 30308 32100 1292 2 RtkAudUService64
1130 19 8656 23988 1328 0 services
71 7 1440 4944 1372 0 LsaIso
2248 31 14880 44312 1384 0 lsass
1323 130 361972 444960 142.64 1456 2 powershell_ise
1397 30 16764 50920 1532 0 svchost
64 11 6248 13572 1552 0 fontdrvhost
227 10 6072 15800 1580 0 WUDFHost
94 22 1784 4956 1664 0 WUDFCompanionHost
480 17 9060 22252 1712 0 WUDFHost

OR

Terminal window
PS C:\Users\PavanKumarBandaru> **Get-Process | Sort-Object Id | Select-Object -First 20**
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
0 0 60 8 0 0 Idle
6584 0 44 128 4 0 System
0 0 176 111512 212 0 Secure System
0 21 14352 55324 256 0 Registry
1445 67 94608 16536 7.41 656 2 ms-teams
58 4 1272 1712 772 0 smss
186 41 12104 21220 976 0 svchost
1015 36 2536 8056 1108 0 csrss
167 12 1688 9980 1212 0 wininit
340 39 12148 30476 1.42 1264 2 taskhostw
393 17 30308 32100 1292 2 RtkAudUService64
1130 19 8348 23680 1328 0 services
71 7 1440 4944 1372 0 LsaIso
2246 32 14936 44280 1384 0 lsass
1330 130 360840 444364 145.06 1456 2 powershell_ise
1400 30 16860 50972 1532 0 svchost
64 11 6248 13572 1552 0 fontdrvhost
227 10 6072 15800 1580 0 WUDFHost
94 22 1844 4988 1664 0 WUDFCompanionHost
480 17 9060 22252 1712 0 WUDFHost