Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

20.18 Interact with Alternate Data Streams

Problem

You want to access and manage the alternate data streams associated with a file.

Solution

Use the -Stream parameter of the Get-Item, Get-Content, Remove-Item, Set-Content, Clear-Content, and Add-Content cmdlets:

PS C:\Downloads > Get-Item * -Stream Zone.Identifier -ErrorAction Ignore |
    Select Filename, Length | Format-Table -Auto

FileName                           Length
--------                           ------
C:\Downloads\a.zip                 26
C:\Downloads\b.exe                 26
C:\Downloads\c.txt                 26

PS > Get-Content .\a.zip -Stream Zone.Identifier
[ZoneTransfer]
ZoneId=3

Additionally, use the colon syntax to name a specific stream in a filename:

PS C:\Downloads > Get-Content .\a.zip:Zone.Identifier
[ZoneTransfer]
ZoneId=3

Discussion

In addition to storing the basic content of files, Windows supports a mechanism called alternate data streams to store additional metadata about these files.

PS > Get-Item .\a.zip -Stream *

   FileName: C:\Downloads\a.zip

Stream                   Length
------                   ------
:$DATA                  6878348
Zone.Identifier              26

The :$DATA stream represents the content you normally see when you open a file.

In this example, the file has an additional alternate data stream, called Zone.Identifier. When you download a file from the internet, many web browsers, email clients, and chat programs add a marker to the file that identifies it as having come from the internet. They place this marker in the Zone.Identifier alternate data stream.

To place your own content in a stream, you can use the Set-Content cmdlet:

PS > Set-Content .\a.zip:MyCustomStream -Value "Hello World"
PS > Get-Item .\a.zip -Stream *

   FileName: C:\Downloads\a.zip

Stream                   Length
------                   ------
:$DATA                  6878348
MyCustomStream               13
Zone.Identifier              26


PS > Get-Content .\a.zip:MyCustomStream
Hello World

While it’s an attractive idea to store additional data in alternate data streams, you should use them with caution. Many programs are unaware of alternate data streams and unintentionally remove them when copying or modifying the file. For example, transferring a file over Remote Desktop or FTP doesn’t retain the alternate data streams. Additionally, they’re not retained when you copy files to filesystems based on the FAT32 format—USB keys being the most common example.

By far, our most frequent brush with alternate data streams comes from the warning generated by Windows and PowerShell when a file has been downloaded from the internet. To learn how to remove this warning, see Recipe 20.17.

See Also

Recipe 20.17, “Unblock a File”