end of 6.File_Management

This commit is contained in:
2026-01-10 12:46:49 +01:00
parent a7849b8b50
commit 63ebdb74ba

View File

@@ -65,3 +65,128 @@ $fileSize = Get-ChildItem -Path "C:\mrsh\test.ps1" -Recurse -Force | Measure-Obj
$fileSize.Sum/1KB $fileSize.Sum/1KB
``` ```
## Environement Variable
Environment variables in PowerShell are used to store system-wide
or user-specific values that programs and scripts can access during execution.
They help manage configuration settings such as file paths, system behavior,
or application options without hard-coding them into scripts.
In PowerShell, environment variables are accessed through the `Env:` drive,
for example `$Env:PATH`, and can be created or modified using simple assignment.
This makes scripts more flexible, portable, and easier to maintain
across different systems.
```powershell
$env:ProgramFiles
$env:ProgramData
$env:SystemDrive
$env:SystemRoot
```
## Match Replace
```powershell
$string = "Jello world!"
if($string -match "Hello"){
Write-Output "The string matches the pattern!"
$newString = $string -replace "Hello" , "Hi"
Write-Output $newString
}
```
## TextFile
Text files are commonly used to store and exchange data in a simple,
readable format.
### Set-Content
The `Set-Content` cmdlet is used to create a new text file
or overwrite an existing one with specified content.
```powershell
Set-Content -Path "C:\mrsh\endor.txt" -Value "Ewoks"
```
### Get-Content
The `Get-Content` cmdlet is used to read the contents of a text file,
returning its data line by line, which is useful for processing or analyzing text.
```powershell
Get-Content -Path "./endor.txt"
```
### Add-Content
```powershell
Add-Content -Path "C:\mrsh\endor.txt" -Value "Jedi"
```
## CSV
In PowerShell, CSV (Comma-Separated Values) files are widely used for storing and
exchanging structured data such as tables and lists.
### Import-CSV
PowerShell provides the `Import-Csv` cmdlet to read CSV files
and convert each row into an object with properties based on the column headers,
making the data easy to work with.
```powershell
Import-CSV -Path ".\Simpsons.csv"
```
### Export-CSV
The `Export-Csv` cmdlet is used to save objects back into a CSV file.
This object-based approach allows for efficient data manipulation,
filtering, and reporting.
```powershell
Get-ComputerInfo | Export-CSV -Path "C:\mrsh.com\same-simpson.csv"
```
## JSON
In PowerShell, JSON (JavaScript Object Notation) is commonly used to
store and exchange structured data in a lightweight,
human-readable format.
This makes it easy to work with APIs, configuration files,
and complex data structures. JSON support in PowerShell enables seamless data interchange and efficient automation.
### ConvertFrom-Json
The `ConvertFrom-Json` cmdlet to transform JSON data into PowerShell objects.
```powershell
$json = Get-Content -Path ".\simpsons.json" -Raw
$data = ConvertFrom-Json -InputObject $json
```
### ConvertTo-Json
and `ConvertTo-Json` to convert objects back into JSON format.
```powershell
Get-NetAdapter | ConvertTo-Json | Out-File ".\NetworkAdapter.json"
```
## XML
In PowerShell, XML is used to store and manage structured data in a hierarchical
format.
XML data is represented as objects, making it easy to navigate,
extract, and modify elements and attributes.
This makes XML useful for working with configuration files,
system data, and applications that rely on structured document formats.
```powershell
$xml_file = Get-Content C:\mrsh\simpsons.xml
$xml_file.simpsons.charcter[0]
$xml_file.save("C:\mrsh\simpsons-nex.xml")
```