4.6 KiB
File Management with PowerShell
Get-ChildItem
Get the content of a curent directory Alias = ls
Get-ChildItem
Get-ChildItem C:\Windows
Set-Location
Change current directory Alias = cd
Set-Location
ACL
Windows Access Control Lists (ACLs) are a core security mechanism used to control access to files, folders, and other system objects. An ACL defines which users or groups are allowed or denied specific permissions, such as read, write, modify, or execute. Each ACL is made up of Access Control Entries (ACEs) that explicitly grant or restrict these permissions. By using ACLs, Windows ensures that only authorized users can access or modify resources, helping to protect system integrity and sensitive data.
Get-Acl
for listing permision for a file
Get-ACL "C:\mrsh\test.ps1"
$file = Get-ACL
$file.Access
Get-ACL "C:\mrsh\test.ps1" | Format-List
Set-ACL
$acl = Get-Acl "C:\mrsh\test.ps1"
$permission = "Administrator", "FullControl", "Allow"
$accessRule = New=Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
Set-ACL "C:\mrsh\test.ps1" $acl
File size
$fileSize = Get-ChildItem -Path "C:\mrsh\test.ps1" -Recurse -Force | Measure-Object -Property Length -Sum
$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.
$env:ProgramFiles
$env:ProgramData
$env:SystemDrive
$env:SystemRoot
Match Replace
$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.
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.
Get-Content -Path "./endor.txt"
Add-Content
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.
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.
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.
$json = Get-Content -Path ".\simpsons.json" -Raw
$data = ConvertFrom-Json -InputObject $json
ConvertTo-Json
and ConvertTo-Json to convert objects back into JSON format.
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.
$xml_file = Get-Content C:\mrsh\simpsons.xml
$xml_file.simpsons.charcter[0]
$xml_file.save("C:\mrsh\simpsons-nex.xml")