PowerShell i magia pracy z obiektami — praktyczne wprowadzenie

PowerShell i magia pracy z obiektami

Czym jest obiekt?

Właściwości obiektu

PS C:\Users\Admin> Get-Process

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     10     2.92      12.04       0.00    8304   0 AggregatorHost
     11     1.86       9.81      19.48    1492   1 ApMsgFwd
      9     1.23       7.32       0.12    2956   1 ApntEx
     17     3.51      20.19       5.42    4592   1 Apoint
     17    11.23      32.66       0.70   15660   1 AppActions
PS C:\Users\Admin> Get-Process -Name Notepad | Select *

Name                       : Notepad
Id                         : 18676
PriorityClass              : Normal
FileVersion                : 11.2507.26.0
HandleCount                : 1165
WorkingSet                 : 132481024
PagedMemorySize            : 62488576
PrivateMemorySize          : 62488576
VirtualMemorySize          : 630501376
TotalProcessorTime         : 00:00:01.9687500
SI                         : 1
Handles                    : 1165
VM                         : 2203948724224
WS                         : 132481024
PM                         : 62488576
NPM                        : 47640
Path                       : C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2507.26.0_x64__8wekyb
                             3d8bbwe\Notepad\Notepad.exe
CommandLine                : "C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2507.26.0_x64__8weky
                             b3d8bbwe\Notepad\Notepad.exe"
Parent                     : System.Diagnostics.Process (explorer)
Company                    : Microsoft Corporation
CPU                        : 1.96875
ProductVersion             : 11.2507.26.0
Description                : Notepad.exe
Product                    : Notepad
__NounName                 : Process
SafeHandle                 : Microsoft.Win32.SafeHandles.SafeProcessHandle
Handle                     : 3136
BasePriority               : 8
ExitCode                   :
HasExited                  : False
StartTime                  : 22/09/2025 13:03:38
ExitTime                   :
MachineName                : .
MaxWorkingSet              : 1413120
MinWorkingSet              : 204800
Modules                    : {System.Diagnostics.ProcessModule (Notepad.exe),
                             System.Diagnostics.ProcessModule (ntdll.dll),
                             System.Diagnostics.ProcessModule (KERNEL32.DLL),
                             System.Diagnostics.ProcessModule (KERNELBASE.dll)…}
NonpagedSystemMemorySize64 : 47640
NonpagedSystemMemorySize   : 47640
PagedMemorySize64          : 62488576
PagedSystemMemorySize64    : 903400
PagedSystemMemorySize      : 903400
PeakPagedMemorySize64      : 64176128
PeakPagedMemorySize        : 64176128
PeakWorkingSet64           : 135647232
PeakWorkingSet             : 135647232
PeakVirtualMemorySize64    : 2203951214592
PeakVirtualMemorySize      : 632991744
PriorityBoostEnabled       : True
PrivateMemorySize64        : 62488576
ProcessorAffinity          : 255
SessionId                  : 1
StartInfo                  :
Threads                    : {16588, 18760, 9588, 19256…}
VirtualMemorySize64        : 2203948724224
EnableRaisingEvents        : False
StandardInput              :
StandardOutput             :
StandardError              :
WorkingSet64               : 132481024
SynchronizingObject        :
MainModule                 : System.Diagnostics.ProcessModule (Notepad.exe)
PrivilegedProcessorTime    : 00:00:00.9687500
UserProcessorTime          : 00:00:01
ProcessName                : Notepad
MainWindowHandle           : 197660
MainWindowTitle            : *# Rola - Notepad
Responding                 : True
Site                       :
Container                  :

Metody obiektu

PS C:\Users\Admin> (Get-Process -Name Notepad).kill()
PS C:\Users\Admin> "Hello PowerShell".ToUpper()
HELLO POWERSHELL

Jak odkrywać właściwości i metody

PS C:\Users\Admin> "Hello PowerShell" | Get-Member

   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),…
Contains             Method                bool Contains(string value), bool Contains(string value, System…
CopyTo               Method                void CopyTo(int sourceIndex, char[] destination, int destinatio…
EndsWith             Method                bool EndsWith(string value), bool EndsWith(string value, System…
EnumerateRunes       Method                System.Text.StringRuneEnumerator EnumerateRunes()
Equals               Method                bool Equals(System.Object obj), bool Equals(string value), bool…
GetEnumerator        Method                System.CharEnumerator GetEnumerator(), System.Collections.IEnum…
GetHashCode          Method                int GetHashCode(), int GetHashCode(System.StringComparison comp…
GetPinnableReference Method                System.Char&, System.Private.CoreLib, Version=9.0.0.0, Culture=…
GetType              Method                type GetType()
GetTypeCode          Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.Get…
IndexOf              Method                int IndexOf(char value), int IndexOf(char value, int startIndex…
IndexOfAny           Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int …
Insert               Method                string Insert(int startIndex, string value)
IsNormalized         Method                bool IsNormalized(), bool IsNormalized(System.Text.Normalizatio…
LastIndexOf          Method                int LastIndexOf(char value), int LastIndexOf(char value, int st…
LastIndexOfAny       Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] any…
Normalize            Method                string Normalize(), string Normalize(System.Text.NormalizationF…
PadLeft              Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, …
PadRight             Method                string PadRight(int totalWidth), string PadRight(int totalWidth…
Remove               Method                string Remove(int startIndex, int count), string Remove(int sta…
Replace              Method                string Replace(string oldValue, string newValue, bool ignoreCas…
ReplaceLineEndings   Method                string ReplaceLineEndings(), string ReplaceLineEndings(string r…
Split                Method                string[] Split(char separator, System.StringSplitOptions option…
StartsWith           Method                bool StartsWith(string value), bool StartsWith(string value, Sy…
Substring            Method                string Substring(int startIndex), string Substring(int startInd…
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 le…
ToDateTime           Method                datetime IConvertible.ToDateTime(System.IFormatProvider provide…
ToDecimal            Method                decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble             Method                double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16              Method                short 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 provi…
ToType               Method                System.Object IConvertible.ToType(type conversionType, System.I…
ToUInt16             Method                ushort IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32             Method                uint IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64             Method                ulong IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper              Method                string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant     Method                string ToUpperInvariant()
Trim                 Method                string Trim(), string Trim(char trimChar), string Trim(Params c…
TrimEnd              Method                string TrimEnd(), string TrimEnd(char trimChar), string TrimEnd…
TrimStart            Method                string TrimStart(), string TrimStart(char trimChar), string Tri…
TryCopyTo            Method                bool TryCopyTo(System.Span[char] destination)
Chars                ParameterizedProperty char Chars(int index) {get;}
Length               Property              int Length {get;}

Praktyczne przykłady

Przykład 1: Filtrowanie procesów po właściwości

PS C:\Users\Admin> Get-Process | Where-Object {$_.WorkingSet -gt 100MB}

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     38   266.08     242.22     437.17    5728   1 chrome
     28   258.65     112.46      80.14    6080   1 chrome
     70   408.36     399.82   1,082.00    8580   1 chrome
     64   808.82     891.97     422.73   15528   1 chrome
     54   530.77     615.26     166.97   19224   1 chrome
     36   165.71     169.49     686.17   19396   1 chrome
     29    99.02     138.05      28.34   20016   1 chrome
     51    94.12     125.94     252.97   21892   1 chrome
     67   520.44     504.90     594.91   22476   1 chrome
     41   229.41     297.38      91.19   22480   1 chrome
    111   408.73     506.27   1,724.89   22676   1 chrome
     49   278.66     285.02     142.50   22872   1 chrome
     48   244.73     326.98      72.81   24424   1 chrome
     33   118.66     163.97     909.27   24468   1 chrome
     28    57.64     121.24       3.05   25680   1 chrome
     70   160.70     102.47       0.00    1692   1 dwm
    115   149.25     221.16     234.41    1592   1 explorer
      0     3.01     836.99       0.00    3820   0 Memory Compression
     37   207.05     178.70     107.61   10952   1 msedge
     55   108.16     168.52     183.91   22848   1 msedge
    239   340.23     243.40       0.00    5872   0 MsMpEng
    172   132.34     114.06   1,399.73   16200   1 PAD.Console.Host
     96    51.64     148.08       5.41   26692   1 PhoneExperienceHost
     67    36.42     102.76       1.45   28548   1 pwsh

Przykład 2: Restart usługi

PS C:\Windows\System32> Get-Service -Name "wuauserv"

Status   Name               DisplayName
------   ----               -----------
Running  wuauserv           Windows Update

PS C:\Windows\System32> (Get-Service -Name "wuauserv").Stop()
PS C:\Windows\System32> Get-Service -Name "wuauserv"

Status   Name               DisplayName
------   ----               -----------
Stopped  wuauserv           Windows Update

PS C:\Windows\System32> (Get-Service -Name "wuauserv").Start()
PS C:\Windows\System32> Get-Service -Name "wuauserv"

Status   Name               DisplayName
------   ----               -----------
Running  wuauserv           Windows Update

Przykład 3: Manipulacja tekstem

PS C:\Users\Admin> "Hello PowerShell".Replace("PowerShell", "Adam")
Hello Adam

Podsumowanie


To wszystko na dziś!

Jeśli masz ciekawe spostrzeżenia lub doświadczenia w tym temacie – koniecznie podziel się nimi w komentarzach.
A jeśli moje materiały są dla Ciebie pomocne, możesz postawić mi wirtualną kawę.

Dzięki za wsparcie!

Postaw mi kawę na buycoffee.to

Adam Pietrzak

Trener IT | Autor szkoleń | Entuzjasta PowerShella

Administrator systemów i sieci wsparcia działań wojskowych z ponad 10-letnim doświadczeniem. Praktyk w dziedzinie bezpieczeństwa systemu Windows, automatyzacji zadań (PowerShell) oraz rozwiązań chmurowych. Trener i twórca materiałów edukacyjnych (szkolenia, warsztaty, artykuły, podręczniki). Pasjonat dzielenia się wiedzą i wspierania początkujących administratorów IT. Prywatnie – amator aktywnego wypoczynku i rodzinnych podróży.


PowerShell - 10 sposobów na optymalizację codziennej pracy
Podziel się

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *