Filtruj, sortuj, wybieraj, czyli praktyczne operacje na danych

Praktyczne operacje na danych

Filtrowanie danych — Where-Object

PS C:\Users\Admin> Get-Service | Where-Object {$_.Status -eq 'Running'}

Status   Name               DisplayName
------   ----               -----------
Running  ApHidMonitorServi… AlpsAlpine HID Monitor Service
Running  Appinfo            Application Information
Running  AppXSvc            AppX Deployment Service (AppXSVC)
Running  AudioEndpointBuil… Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Servi…
Running  BluetoothUserServ… Usługa obsługi użytkownika protokołu …
Running  BrokerInfrastruct… Background Tasks Infrastructure Servi…
Running  BrYNSvc            BrYNSvc
PS C:\Users\Admin> Get-Service | Where-Object Status -eq Running

Status   Name               DisplayName
------   ----               -----------
Running  ApHidMonitorServi… AlpsAlpine HID Monitor Service
Running  Appinfo            Application Information
Running  AppXSvc            AppX Deployment Service (AppXSVC)
Running  AudioEndpointBuil… Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Servi…
Running  BluetoothUserServ… Usługa obsługi użytkownika protokołu …
Running  BrokerInfrastruct… Background Tasks Infrastructure Servi…
Running  BrYNSvc            BrYNSvc
PS C:\Users\Admin> Get-Service | Where-Object {$_.Status -eq 'Running' -and $_.Name -like 'A*'}

Status   Name               DisplayName
------   ----               -----------
Running  ApHidMonitorServi… AlpsAlpine HID Monitor Service
Running  Appinfo            Application Information
Running  AppXSvc            AppX Deployment Service (AppXSVC)
Running  AudioEndpointBuil… Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
PS C:\Users\Admin> Get-ChildItem -Path "C:\Test" -Filter *.txt

    Directory: C:\Test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          15/09/2023    14:21              0 Nowa_Nazwa_01.txt
-a---          15/09/2023    14:21              0 Nowa_Nazwa_02.txt
-a---          15/09/2023    13:21              0 Nowa_Nazwa_03.txt
-a---          15/09/2023    13:21              0 Nowa_Nazwa_04.txt
-a---          15/09/2023    14:39              0 tekst.txt
-a---          15/09/2023    14:38             56 tekst2.txt
PS C:\Users\Admin> $services = Get-Service
PS C:\Users\Admin> $services.Where({$_.status -eq 'Running'})

Status   Name               DisplayName
------   ----               -----------
Running  ApHidMonitorSer... AlpsAlpine HID Monitor Service
Running  Appinfo            Application Information
Running  AppXSvc            AppX Deployment Service (AppXSVC)
Running  AudioEndpointBu... Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Ser...
Running  BluetoothUserSe... Usługa obsługi użytkownika protokoł...
Running  BrokerInfrastru... Background Tasks Infrastructure Ser...
Running  BrYNSvc            BrYNSvc

Sortowanie danych — Sort-Object

PS C:\Users\Admin> Get-ChildItem | Sort-Object -Property Length -Descending


    Directory: C:\Users\Admin


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        06/06/2024     08:46         113606 raport_DELL.html
-a----        02/06/2024     23:20         104572 raport_.html
-a----        05/06/2024     22:17          54076 proc.htm
-a----        06/06/2024     09:46          19608 procesy.html
-a----        17/01/2025     14:44          19598 ServiceLog.txt
-a----        18/09/2023     09:14           6193 soft.csv
-a----        05/06/2024     09:48           5452 users_.csv
-a----        27/06/2024     20:00           5452 AD_users.csv
-a----        27/06/2024     20:07           5202 ADusers2.csv
-a----        27/06/2024     20:09           4952 ADusers.csv
-a----        07/02/2022     11:16           4921 users.csv
-a----        06/06/2024     08:45           2734 ComputerReport.html
-a----        06/06/2024     09:50           1310 computer.html
PS C:\Users\Admin> Get-Service | Sort-Object -Property Status, DisplayName

Status   Name               DisplayName
------   ----               -----------
Stopped  AxInstSV           ActiveX Installer (AxInstSV)
Stopped  ADPSvc             ADPSvc
Stopped  AarSvc_124270      Agent Activation Runtime_124270
Stopped  AppReadiness       App Readiness
Stopped  AppIDSvc           Application Identity
Stopped  ALG                Application Layer Gateway Service
Stopped  AppMgmt            Application Management
Stopped  AssignedAccessM... AssignedAccessManager Service
Stopped  tzautoupdate       Auto Time Zone Updater
Stopped  BEService          BattlEye Service
Stopped  BDESVC             BitLocker Drive Encryption Service
Stopped  wbengine           Block Level Backup Engine Service
Stopped  PeerDistSvc        BranchCache

Wybieranie właściwości — Select-Object

PS C:\Users\Admin> Get-Process | Select-Object Name, CPU, WorkingSet

Name                             CPU         WorkingSet
----                             ---         ----------
AggregatorHost                                  6094848
ApMsgFwd                         360.21875      1970176
ApntEx                           0.1875         1122304
Apoint                           132.171875     3387392
AppActions                       4.359375       4972544
ApplicationFrameHost             5.5           34295808
AppVShNotify                     0.0625         1228800
ApRemote                         0.09375         561152
audiodg                          1.15625       22032384
PS C:\Users\Admin> Get-ChildItem | Select-Object -First 5


    Directory: C:\Users\Admin


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        05/01/2024     21:22                .android
d-----        18/09/2023     16:59                .Azure
d-----        11/09/2025     18:51                .cache
d-----        23/09/2023     12:37                .lim
d-----        20/09/2025     12:25                .n8n
PS C:\Users\Admin> Get-Process | Select-Object -Property ProcessName -Unique

ProcessName
-----------
AggregatorHost
ai
ApMsgFwd
ApntEx
Apoint
AppActions
ApplicationFrameHost
AppVShNotify
PS C:\Users\Admin> Get-ChildItem -File | Select-Object Name, @{Name="Size[KB]";Expression={"{0:N2}" -f ($_.length/1KB)}}

Name                               Size[KB]
----                               --------
ADusers.csv                        4.84
ADusers2.csv                       5.08
AD_users.csv                       5.32
art2.png                           0.35
computer.html                      1.28
ComputerReport.html                2.67

Łączenie filtrowania, sortowania i wyboru

Znajdź 5 największych pliki w katalogu domowym

PS C:\Users\Admin> Get-ChildItem -File | Sort-Object Length -Descending | Select-Object Name, Length -First 5

Name             Length
----             ------
raport_DELL.html 113606
raport_.html     104572
proc.htm          54076
procesy.html      19608
ServiceLog.txt    19598

Dlaczego to takie ważne?

Podsumowanie

Dla ambitnych:


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 *