PowerShell/File System
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]- Wikipedia: File system
- Wikipedia: Directory (computing)
- Wikipedia: Directory structure
- Wikipedia: Text file
- BonusBits: Mastering PowerShell Chapter 15 - The File System
Multimedia
[edit | edit source]- YouTube: Creating Files and Folders in PowerShell
- YouTube: Grab content from a file in Powershell
- 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]- Review Microsoft TechNet: Using the Get-ChildItem Cmdlet. Experiment with using Get-ChildItem to retrieve directory listings.
- Review Microsoft TechNet: Using the New-Item Cmdlet. Experiment with creating folders and files.
- Review Microsoft TechNet: Using the Move-Item Cmdlet. Experiment with moving folders and files.
- Review Microsoft TechNet: Using the Copy-Item Cmdlet. Experiment with copying folders and files.
- Review Microsoft TechNet: Using the Remove-Item Cmdlet. Experiment with removing folders and files.
- 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.
- 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]Assessments
[edit | edit source]- Flashcards: Quizlet: Windows PowerShell - File System
- Quiz: Quizlet: Windows PowerShell - File System
See Also
[edit | edit source]References
[edit | edit source]- ↑ Microsoft TechNet: Get-ChildItem
- ↑ Microsoft TechNet: New-Item
- ↑ Microsoft TechNet: Move-Item
- ↑ Microsoft TechNet: Copy-Item
- ↑ Microsoft TechNet: Remove-Item
- ↑ Microsoft TechNet: Test-Path
- ↑ Microsoft TechNet: Set-Content
- ↑ Microsoft TechNet: Add-Content
- ↑ Microsoft TechNet: Get-Content
- ↑ Microsoft TechNet: Clear-Content
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: File system
- ↑ Wikipedia: Directory (computing)
- ↑ Wikipedia: Text file
- ↑ Wikipedia: Text file
- ↑ Microsoft TechNet: Get-ChildItem
- ↑ Microsoft TechNet: New-Item
- ↑ Microsoft TechNet: Move-Item
- ↑ Microsoft TechNet: Copy-Item
- ↑ Microsoft TechNet: Remove-Item
- ↑ Microsoft TechNet: Test-Path
- ↑ Microsoft TechNet: Set-Content
- ↑ Microsoft TechNet: Add-Content
- ↑ Microsoft TechNet: Get-Content
- ↑ Microsoft TechNet: Clear-Content
- ↑ Wikipedia: ASCII
- ↑ Wikipedia: Metadata
- ↑ Wikipedia: File system fragmentation
- ↑ Wikipedia: File system
- ↑ Wikipedia: Directory (computing)
- ↑ Wikipedia: Metadata
- ↑ Wikipedia: Directory (computing)
- ↑ Wikipedia: Metadata
- ↑ Wikipedia: Directory (computing)