Jump to content

PowerShell/File System

From Wikiversity

This lesson introduces PowerShell file system processing.

Objectives and Skills

[edit | edit source]

After completing this lesson, you will be able to:

  • Describe basic PowerShell file system concepts.
  • Create PowerShell scripts to manage files and folders.
  • Create PowerShell scripts to read and write text file content.

Readings

[edit | edit source]
  1. Wikipedia: File system
  2. Wikipedia: Directory (computing)
  3. Wikipedia: Directory structure
  4. Wikipedia: Text file
  5. BonusBits: Mastering PowerShell Chapter 15 - The File System

Multimedia

[edit | edit source]
  1. YouTube: Creating Files and Folders in PowerShell
  2. YouTube: Grab content from a file in Powershell
  3. YouTube: PowerShell - How To - Working with TXT and CSV Files

Examples

[edit | edit source]

Get-ChildItem

[edit | edit source]

The Get-ChildItem cmdlet gets items and child items from a given location.[1] It is similar in concept to a DIR command to list files in a directory.

Get-ChildItem -Path 'C:\'             # List all files in the root directory.
Get-ChildItem -Path 'C:\' -Recurse    # List all files on the C drive.

New-Item

[edit | edit source]

The New-Item cmdlet creates a new item of the given type at the given path location.[2]

New-Item -Path 'C:\' -Name 'PSTest' -ItemType Directory
New-Item -Path 'C:\PSTest' -Name 'Test.txt' -ItemType File -Value 'This is a test.'

Move-Item

[edit | edit source]

The Move-Item cmdlet moves an item from the given path location to the given destination.[3]

Move-Item -Path 'C:\PSTest\Test.txt' -Destination 'C:\PSTest\Test2.txt'

Copy-Item

[edit | edit source]

The Copy-Item cmdlet copies an item from the given path location to the given destination.[4]

Copy-Item -Path 'C:\PSTest\Test2.txt' -Destination 'C:\PSTest\Test.txt'

Remove-Item

[edit | edit source]

The Remove-Item cmdlet removes an item from the given path location.[5]

Remove-Item -Path 'C:\PSTest\Test.txt'
Remove-Item -Path 'C:\PSTest' -Recurse

Test-Path

[edit | edit source]

The Test-Path cmdlet determines whether a path exists.[6]

$filename = 'example.txt'
if((Test-Path $filename) -ne $true)
{
    Write-Output "$filename not found!"
}

Set-Content

[edit | edit source]

The Set-Content cmdlet is a string-processing cmdlet that writes or replaces the content in the specified item, such as a file.[7]

$path = $HOME + '\My PowerShell Content.txt'
$value =  @('Colors', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet')
Set-Content -Path $path -Value $value

Add-Content

[edit | edit source]

The Add-Content cmdlet appends content to a specified item or file.[8]

$path = $HOME + '\My PowerShell Content.txt'
Add-Content -Path $path -Value 'Brown'
Add-Content -Path $path -Value 'Black'

Get-Content

[edit | edit source]

The Get-Content cmdlet gets the content of a given item.[9]

$path = $HOME + '\My PowerShell Content.txt'
$content = Get-Content -Path $path
foreach ($line in $content)
{
    Write-Output $line
}

Clear-Content

[edit | edit source]

The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but it does not delete the item.[10]

$path = $HOME + '\My PowerShell Content.txt'
Clear-Content -Path $path

Activities

[edit | edit source]
  1. Review Microsoft TechNet: Using the Get-ChildItem Cmdlet. Experiment with using Get-ChildItem to retrieve directory listings.
  2. Review Microsoft TechNet: Using the New-Item Cmdlet. Experiment with creating folders and files.
  3. Review Microsoft TechNet: Using the Move-Item Cmdlet. Experiment with moving folders and files.
  4. Review Microsoft TechNet: Using the Copy-Item Cmdlet. Experiment with copying folders and files.
  5. Review Microsoft TechNet: Using the Remove-Item Cmdlet. Experiment with removing folders and files.
  6. Review Microsoft TechNet: Using the Get-Content Cmdlet. Use the Get-Content cmdlet to read a file and process the file content line by line. For example, create a file with student names and test scores. Process the file line by line and display the average score for the class. Use Test-Path to verify that the file path exists before getting the file content, and a Try-Catch-Finally block to handle any errors that occur during file processing.
  7. Review WindowsITPro: Get Hex Dumps of Files in PowerShell. Use the Get-Content cmdlet and with -Encoding Byte and -ReadCount 1 to read one character at a time and then display each character's ASCII value. Display a multi-line text file and verify that each line does, indeed, end with CR and LF characters (13 and 10).

Lesson Summary

[edit | edit source]
  • A file system is used to control how data is stored and retrieved. There are many different kinds of file systems. Each one has different structure and logic, properties of speed, flexibility, security, size and more.[11]
  • File systems are responsible for arranging storage space; reliability, efficiency, and tuning with regard to the physical storage medium are important design considerations.[12]
  • File systems allocate space in a granular manner, usually multiple physical units on the device.[13]
  • A filename (or file name) is used to identify a storage location in the file system.[14]
  • File systems typically have directories (also called folders) which allow the user to group files into separate collections.[15]
  • A file system stores all the metadata associated with the file—including the file name, the length of the contents of a file, and the location of the file in the folder hierarchy—separate from the contents of the file.[16]
  • Directory utilities may be used to create, rename and delete directory entries.[17]
  • File utilities create, list, copy, move and delete files, and alter metadata.[18]
  • All file systems have some functional limit that defines the maximum storable data capacity within that system.[19]
  • A directory is a file system cataloging structure which contains references to other computer files, and possibly other directories.[20]
  • A text file is a kind of computer file that is structured as a sequence of lines of electronic text.[21]
  • MS-DOS and Windows use a common text file format, with each line of text separated by a two-character combination: CR and LF, which have ASCII codes 13 and 10.[22]
  • The Get-ChildItem cmdlet gets items and child items from a given location.[23]
  • The New-Item cmdlet creates a new item of the given type at the given path location.[24]
  • The Move-Item cmdlet moves an item from the given path location to the given destination.[25]
  • The Copy-Item cmdlet copies an item from the given path location to the given destination.[26]
  • The Remove-Item cmdlet removes an item from the given path location.[27]
  • The Test-Path cmdlet determines whether a path exists.[28]
  • The Set-Content cmdlet is a string-processing cmdlet that writes or replaces the content in the specified item, such as a file.[29]
  • The Add-Content cmdlet appends content to a specified item or file.[30]
  • The Get-Content cmdlet gets the content of a given item.[31]
  • The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but it does not delete the item.[32]

Key Terms

[edit | edit source]
ASCII (American Standard Code for Information Interchange)
A character-encoding scheme originally based on the English alphabet which encodes 128 specified characters into 7-bit binary integers.[33]
descriptive metadata
Data about individual instances of application data, or data content.[34]
file system fragmentation
The inability of a file system to lay out related data sequentially (contiguously)[35]
flat file system
A file system in which there are no subdirectories.[36]
hierarchical filesystem
A filesystem in which files and directories are organized in a manner that resembles a tree.[37]
metadata
Data about data, including structural metadata and descriptive metadata.[38]
root
The top-most directory in a hierarchical filesystem.[39]
structural metadata
Data about the design and specification of data structures, or the containers of data.[40]
subdirectory
A directory contained inside another directory.[41]

Review Questions

[edit | edit source]
Enable JavaScript to hide answers.
Click on a question to see the answer.
1. A file system is used to control _____. There are many different kinds of file systems. Each one has different _____, properties of _____, _____, _____, _____ and more.
A file system is used to control how data is stored and retrieved. There are many different kinds of file systems. Each one has different structure and logic, properties of speed, flexibility, security, size and more.
2. File systems are responsible for _____; _____, _____, and _____ are important design considerations.
File systems are responsible for arranging storage space; reliability, efficiency, and tuning with regard to the physical storage medium are important design considerations.
3. File systems allocate space in a _____ manner, usually with _____.
File systems allocate space in a granular manner, usually with multiple physical units on the device.
4. A filename (or file name) is used to _____.
A filename (or file name) is used to identify a storage location in the file system.
5. File systems typically have directories (also called folders) which _____.
File systems typically have directories (also called folders) which allow the user to group files into separate collections.
6. A file system stores all the metadata associated with the file—including _____, _____, and _____—separate from the contents of the file.
A file system stores all the metadata associated with the file—including the file name, the length of the contents of a file, and the location of the file in the folder hierarchy—separate from the contents of the file.
7. Directory utilities may be used to _____, _____ and _____ directory entries.
Directory utilities may be used to create, rename and delete directory entries.
8. File utilities _____, _____, _____, _____ and _____ files, and alter metadata.
File utilities create, list, copy, move and delete files, and alter metadata.
9. All file systems have some functional limit that defines _____.
All file systems have some functional limit that defines the maximum storable data capacity within that system.
10. A directory is a file system cataloging structure which _____.
A directory is a file system cataloging structure which contains references to other computer files, and possibly other directories.
11. A text file is a kind of computer file that is _____.
A text file is a kind of computer file that is structured as a sequence of lines of electronic text.
12. MS-DOS and Windows use a common text file format, with each line of text separated by _____.
MS-DOS and Windows use a common text file format, with each line of text separated by a two-character combination: CR and LF, which have ASCII codes 13 and 10.
13. The Get-ChildItem cmdlet _____.
The Get-ChildItem cmdlet gets items and child items from a given location.
14. The New-Item cmdlet _____.
The New-Item cmdlet creates a new item of the given type at the given path location.
15. The Move-Item cmdlet _____.
The Move-Item cmdlet moves an item from the given path location to the given destination.
16. The Copy-Item cmdlet _____.
The Copy-Item cmdlet copies an item from the given path location to the given destination.
17. The Remove-Item cmdlet _____.
The Remove-Item cmdlet removes an item from the given path location.
18. The Test-Path cmdlet _____.
The Test-Path cmdlet determines whether a path exists.
19. The Set-Content cmdlet _____.
The Set-Content cmdlet is a string-processing cmdlet that writes or replaces the content in the specified item, such as a file.
20. The Add-Content cmdlet _____.
The Add-Content cmdlet appends content to a specified item or file.
21. The Get-Content cmdlet _____.
The Get-Content cmdlet gets the content of a given item.
22. The Clear-Content cmdlet _____.
The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but it does not delete the item.

Assessments

[edit | edit source]

See Also

[edit | edit source]

References

[edit | edit source]
Type classification: this is a lesson resource.
Completion status: this resource is considered to be complete.