System plików, czyli praca z plikami i katalogami

PowerShell i system plików

Czym jest system plików?

System Plików PowerShell
Przykładowa struktura systemu plików

Dlaczego warto zarządzać plikami i katalogami z poziomu PowerShella?

Podstawowe zadania w pracy z plikami i katalogami

Tworzenie i modyfikacja

PS C:\Users\Admin> New-Item -Path "C:\Temp\Nowy_Plik.txt" -ItemType File


    Directory: C:\Temp


Mode                 LastWriteTime         Length Name         
----                 -------------         ------ ----           
-a----        23/11/2025     15:20              0 Nowy_Plik.txt   
PS C:\Users\Admin> New-Item -Path "C:\Temp\Nowy_Katalog" -ItemType Directory


    Directory: C:\Temp


Mode                 LastWriteTime         Length Name     
----                 -------------         ------ ----          
d-----        23/11/2025     15:20                Nowy_Katalog    
PS C:\Users\Admin> 1..10 | ForEach-Object {Out-File -InputObject  “Plik nr: $_” -FilePath “C:\Temp\Nowy_Katalog\plik$_.txt”} 
PS C:\Users\Admin> Set-Content -Path "C:\Temp\Nowy_Plik.txt" -Value "Nowa treść pliku po aktualizacji."

💡Warto wiedzieć

Set-Content -Path .\DateTime.txt -Value (Get-Date)
PS C:\Users\Admin> ls -Path C:\Temp


    Directory: C:\Temp


Mode                 LastWriteTime         Length Name  
----                 -------------         ------ ----  
d-----        23/11/2025     15:48                Archiwum 
d-----        23/11/2025     15:22                Backup
-a----        26/11/2025     21:13             21 Nowy_Plik.txt  

PS C:\Users\Admin> Set-ItemProperty -Path "C:\Temp\Nowy_Plik.txt" -Name Attributes -Value ReadOnly -Verbose
VERBOSE: Performing the operation "Set Property File" on target "Item: C:\Temp\Nowy_Plik.txt Property: Attributes Value: ReadOnly".

PS C:\Users\Admin> ls -Path C:\Temp


    Directory: C:\Temp


Mode                 LastWriteTime         Length Name  
----                 -------------         ------ ----  
d-----        23/11/2025     15:48                Archiwum 
d-----        23/11/2025     15:22                Backup
--r---        26/11/2025     21:13             21 Nowy_Plik.txt  

Kopiowanie

PS C:\Users\Admin> Copy-Item -Path "C:\Temp\Nowy_Plik.txt" -Destination "C:\Temp\Backup\Nowy_Plik.txt"
PS C:\Users\Admin> Copy-Item -Path "C:\Temp\Nowy_Katalog" -Destination "C:\Temp\Backup\" -Recurse -PassThru


    Directory: C:\Temp\Backup


Mode                 LastWriteTime         Length Name            
----                 -------------         ------ ----           
d-----        23/11/2025     15:22                Nowy_Katalog  


    Directory: C:\Temp\Backup\Nowy_Katalog


Mode                 LastWriteTime         Length Name   
----                 -------------         ------ ---- 
-a----        23/11/2025     15:21             26 plik1.txt  
-a----        23/11/2025     15:21             28 plik10.txt      
-a----        23/11/2025     15:21             26 plik2.txt 
-a----        23/11/2025     15:21             26 plik3.txt 
-a----        23/11/2025     15:21             26 plik4.txt 
-a----        23/11/2025     15:21             26 plik5.txt 
-a----        23/11/2025     15:21             26 plik6.txt  
-a----        23/11/2025     15:21             26 plik7.txt   
-a----        23/11/2025     15:21             26 plik8.txt   
-a----        23/11/2025     15:21             26 plik9.txt 

Przenoszenie

PS C:\Users\Admin> Move-Item -Path "C:\Temp\Nowy_Plik.txt" -Destination "C:\Temp\Archiwum\Nowy_Plik.txt" -PassThru


    Directory: C:\Temp\Archiwum


Mode                 LastWriteTime         Length Name   
----                 -------------         ------ ----     
-a----        23/11/2025     15:20             35 Nowy_Plik.txt  
PS C:\Users\Admin> Move-Item -Path "C:\Temp\Nowy_Katalog" -Destination "C:\Temp\Archiwum\Nowy_Katalog" -PassThru


    Directory: C:\Temp\Archiwum


Mode                 LastWriteTime         Length Name   
----                 -------------         ------ ----    
d-----        23/11/2025     15:21                Nowy_Katalog

Sprawdzanie zawartości katalogów

PS C:\Users\Admin> Move-Item -Path "C:\Temp\Nowy_Katalog" -Destination "C:\Temp\Archiwum\Nowy_Katalog" -PassThru


    Directory: C:\Temp\Archiwum


Mode                 LastWriteTime         Length Name   
----                 -------------         ------ ----    
d-----        23/11/2025     15:21                Nowy_Katalog       



PS C:\Users\Admin> Get-ChildItem -Path "C:\Temp" -Recurse


    Directory: C:\Temp


Mode                 LastWriteTime         Length Name        
----                 -------------         ------ ----      
d-----        23/11/2025     15:24                Archiwum     
d-----        23/11/2025     15:22                Backup          


    Directory: C:\Temp\Archiwum


Mode                 LastWriteTime         Length Name    
----                 -------------         ------ ----    
d-----        23/11/2025     15:21                Nowy_Katalog     
-a----        23/11/2025     15:20              35 Nowy_Plik.txt     


    Directory: C:\Temp\Archiwum\Nowy_Katalog


Mode                 LastWriteTime         Length Name      
----                 -------------         ------ ----      
-a----        23/11/2025     15:21             26 plik1.txt   
-a----        23/11/2025     15:21             28 plik10.txt  
-a----        23/11/2025     15:21             26 plik2.txt  
-a----        23/11/2025     15:21             26 plik3.txt    
-a----        23/11/2025     15:21             26 plik4.txt    
-a----        23/11/2025     15:21             26 plik5.txt  
-a----        23/11/2025     15:21             26 plik6.txt 
-a----        23/11/2025     15:21             26 plik7.txt  
-a----        23/11/2025     15:21             26 plik8.txt 
-a----        23/11/2025     15:21             26 plik9.txt   


    Directory: C:\Temp\Backup


Mode                 LastWriteTime         Length Name     
----                 -------------         ------ ----     
d-----        23/11/2025     15:22                Nowy_Katalog   
-a----        23/11/2025     15:20             35 Nowy_Plik.txt  


    Directory: C:\Temp\Backup\Nowy_Katalog


Mode                 LastWriteTime         Length Name            
----                 -------------         ------ ----         
-a----        23/11/2025     15:21             26 plik1.txt  
-a----        23/11/2025     15:21             28 plik10.txt   
-a----        23/11/2025     15:21             26 plik2.txt    
-a----        23/11/2025     15:21             26 plik3.txt     
-a----        23/11/2025     15:21             26 plik4.txt    
-a----        23/11/2025     15:21             26 plik5.txt    
-a----        23/11/2025     15:21             26 plik6.txt     
-a----        23/11/2025     15:21             26 plik7.txt     
-a----        23/11/2025     15:21             26 plik8.txt      
-a----        23/11/2025     15:21             26 plik9.txt 
PS C:\Users\Admin> Get-ChildItem -Path "C:\Temp" -Filter "*.txt" -Recurse


    Directory: C:\Temp\Archiwum


Mode                 LastWriteTime         Length Name     
----                 -------------         ------ ----         
-a----        23/11/2025     15:20              35 Nowy_Plik.txt                                                                                                                 


    Directory: C:\Temp\Archiwum\Nowy_Katalog


Mode                 LastWriteTime         Length Name                                                                                                                          
----                 -------------         ------ ----                                                            
-a----        23/11/2025     15:21             26 plik1.txt                                                                                                                     
-a----        23/11/2025     15:21             28 plik10.txt                                                                                                                    
-a----        23/11/2025     15:21             26 plik2.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik3.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik4.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik5.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik6.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik7.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik8.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik9.txt                                                                                                                     


    Directory: C:\Temp\Backup


Mode                 LastWriteTime         Length Name   
----                 -------------         ------ ----       
-a----        23/11/2025     15:20             35 Nowy_Plik.txt                                                                                                                 


    Directory: C:\Temp\Backup\Nowy_Katalog


Mode                 LastWriteTime         Length Name     
----                 -------------         ------ ----          
-a----        23/11/2025     15:21             26 plik1.txt                                                                                                                     
-a----        23/11/2025     15:21             28 plik10.txt                                                                                                                    
-a----        23/11/2025     15:21             26 plik2.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik3.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik4.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik5.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik6.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik7.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik8.txt                                                                                                                     
-a----        23/11/2025     15:21             26 plik9.txt
PS C:\Users\Admin> Get-ChildItem -Path "C:\Temp" -Recurse -Directory | 
Where-Object {$_.GetFiles().Count -eq 0 –and $_.GetDirectories().Count -eq 0 } | 
Select-Object Fullname

FullName             
--------             
C:\Temp\Stary_Katalog

Usuwanie

PS C:\Users\Admin> Remove-Item -Path "C:\Temp\Backup\Nowy_Plik.txt" -Verbose
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Plik.txt".
PS C:\Users\Admin> Remove-Item -Path "C:\Temp\Archiwum\Nowy_Katalog\*.txt" -Confirm

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik1.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik10.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik2.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik3.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik4.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik5.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik6.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik7.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik8.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Temp\Archiwum\Nowy_Katalog\plik9.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
PS C:\Users\Admin> Remove-Item -Path "C:\Temp\Backup" -Recurse -Verbose
VERBOSE: Performing the operation "Remove Directory" on target "C:\Temp\Backup".
VERBOSE: Performing the operation "Remove Directory" on target "C:\Temp\Backup\Nowy_Katalog".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik1.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik10.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik2.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik3.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik4.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik5.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik6.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik7.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik8.txt".
VERBOSE: Performing the operation "Remove File" on target "C:\Temp\Backup\Nowy_Katalog\plik9.txt".

Dobre praktyki

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

Administrator | Trener i autor szkoleń | Entuzjasta PowerShella

Administrator systemów i sieci wsparcia działań wojskowych z ponad 10-letnim doświadczeniem. Praktyk w dziedzinie zarządzania Active Directory, bezpieczeństwa systemu Windows oraz automatyzacji zadań (PowerShell). 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 *