Files, Paths, and Disks
`-Path` versus `-LiteralPath` Options
Many of the commands that deal with input files accept two options to pass files -Path
and -LiteralPath
. They are mutually exclusive, and almost always -Path
is the default and is the most heavily used. The advantage of -LiteralPath
is that you can use reserved characters with it (e.g. [,],*
) and they will not expand, or create other problems. Keep in mind that values that come from the pipeline, use the -Path
option.
A rather inconvenient result of the two options is that if you want to provide the same interface in your scripts, or extend system commands (e.g. Get-ChildItem
) you will need to deal with both of them.
Symbolic Links, Junctions, and Hard Links
For reference on the three concepts read:
- Hard Links and Junctions from MSDN
- Creating Symbolic Links from MSDN
TODO: Explain differences between them
TODO: Show PowerShell commands to deal with them
NTFS Alternate Data Streams (ADS)
See Alternate Data Streams in NTFS from technet.
To see the list of alternate data streams for a file named file.txt
, to get the content of one of those streams (e.g. astream
), to set the content of a stream (e.g. bstream
), or to delete a stream (e.g. dstream
) use:
Get-Item -Path file.txt -Stream *
Get-Content -Path file.txt -Stream astream
Set-Content -Path file.txt -Stream bstream
Remove-Item -Path file.txt -Stream dstream
An interesting ADS
is the Zone.Identifier
which specifies the location from which the file has been downloaded (e.g. Internet versus trusted site). ADS
's are cool, but keep in mind that they do not survive beyond NTFS; i.e., the alternate streams will be lost if you copy to a USB that uses FAT, or if you upload the file to a GIT server.
File Hash
Use the Get-FileHash -Path <Filename>
to compute the hash of a file.
Zip/Unzip Files and Directories
This can be done with the standard .NET
libraries. For example to zip a directory:
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$fullOutput = Join-Path (Get-Location) $output
Another option is to use the New-ZipFile
command from the PowerShellCookbook
module.1
Managing Disks
There is a very nice article for maintaining storage in Essential PowerShell Cmdlets for Auditing and Maintaining Storage. I will summarize the interesting commands. See the article for detailed information.
To get information about the existing disks, use Get-Disk
, Get-PhysicalDisk
, and Get-VirtualDisk
.
The Get-Volume
and Get-Partition
commands give more details about the available modules. The latter is preferable for figure out information about hard disks.
If Get-PhysicalDisk
returns information about a disk for which there are no available volumes, then this means that this disk need to be initialized, partitioned, and formatted (and optionally labeled). These steps can be done as follows (assuming that the uninitialized disk has number 2, and that we create two partitions on that disk):
Initialize-Disk -Number 2
New-Partition -DiskNumber 2 -AssignDriveLetter -Size 500gb
New-Partition -DiskNumber 2 -AssignDriveLetter –UseMaximumSize
Format-Volume -DriveLetter F,G -FileSystem NTFS
Set-Volume -DriveLetter F -NewFileSystemLabel "UserData
To resize a partition use Resize-Partition
, e.g.:
Resize-Partition -DriveLetter G -Size 500gb
To remove a partition use Remove-Partition
, e.g.:
Remove-Partition -DriveLetter G
Managing Disks using Desired State Configuration
See "Desired State Configuration --> Managing Disks".
1. This module contains code from the "Windows PowerShell Cookbook" book, which is actually a really nice source of information about PowerShell. ↩