mirror of
https://github.com/serialexperiments0815/PowershellExcercises.git
synced 2026-07-12 15:32:25 +00:00
Powershell scriping excercise Nr.1
Added: 1. File extension filtered file-counter. 2. System-info display script (OS, CPU, RAM). 3. File listing script. 4. Filtered file renaming script by pattern. 5. Start of to-do-text-file for next commit.
This commit is contained in:
commit
361e384195
5 changed files with 48 additions and 0 deletions
7
1 - Basics/count-files-of-specific-type.ps1
Normal file
7
1 - Basics/count-files-of-specific-type.ps1
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Call 'Get-ChildItem' cmdlet method to retrieve filesystem objects.
|
||||||
|
# './' specifies current directory.
|
||||||
|
# '-Recurse' instructs iterations through subdirectories.
|
||||||
|
# '-File' returns results for files only, no directories.
|
||||||
|
# '-Filter' returns results for specified string pattern.
|
||||||
|
|
||||||
|
Get-ChildItem -Path "./" -Recurse -File -Filter *.ps1
|
||||||
4
1 - Basics/display-system-info.ps1
Normal file
4
1 - Basics/display-system-info.ps1
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Calling 'Get-ComputerInfo' method to return all computer information.
|
||||||
|
Get-ComputerInfo |
|
||||||
|
# Calling 'Select-Object' method to filter results through '-Property' parameters.
|
||||||
|
Select-Object -Property WindowsProductName, OSDisplayVersion, CsProcessors, OsTotalVisibleMemorySize
|
||||||
18
1 - Basics/list-files-in-folder.ps1
Normal file
18
1 - Basics/list-files-in-folder.ps1
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Call 'Get-ChildItem' cmdlet method to retrieve filesystem objects.
|
||||||
|
# './' specifies current directory.
|
||||||
|
# '-Recurse' instructs iterations through subdirectories.
|
||||||
|
# '-File' returns results for files only, no directories.
|
||||||
|
|
||||||
|
Get-ChildItem -Path './' -Recurse -File |
|
||||||
|
|
||||||
|
# Call 'Select-Object' cmdlet method to return specified properties.
|
||||||
|
# '-Property FullName' returns full String path for each object.
|
||||||
|
# '-Property LastWriteTime' returns DateTime for last modification.
|
||||||
|
|
||||||
|
Select-Object -Property FullName, LastWriteTime |
|
||||||
|
|
||||||
|
# Call 'Export-Csv' cmdlet method to convert outputs into CSV file.
|
||||||
|
# '-Path './folderFiles.csv'' returns output of file in current directory.
|
||||||
|
# '-NoTypeInformation' supresses #TYPE metadata line.
|
||||||
|
|
||||||
|
Export-Csv -Path './folderFiles.csv' -NoTypeInformation
|
||||||
19
1 - Basics/rename-files-in-folder-by-pattern.ps1
Normal file
19
1 - Basics/rename-files-in-folder-by-pattern.ps1
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
# Calling 'Get-ChildItem' method for return of items in Folder.
|
||||||
|
# "./" In current directory.
|
||||||
|
# "-Filter *.PNG" Of the type PNG.
|
||||||
|
|
||||||
|
Get-ChildItem "./" -Filter *.PNG |
|
||||||
|
|
||||||
|
# Calling 'ForEach-Object' loop method for iterating through returned files.
|
||||||
|
ForEach-Object {
|
||||||
|
|
||||||
|
# Declaring and defining $newName variable.
|
||||||
|
# Consisting of "{0}/$_.BaseName/Original Name" "_" "{1:D4}/$i/4-digit-counter" "{2}/$_.Extension/original extension"
|
||||||
|
$newName = "{0}_{1:D4}{2}" -f $_.BaseName, $i, $_.Extension
|
||||||
|
|
||||||
|
# Calling 'Rename-Item" method returns updated filename.
|
||||||
|
Rename-Item $_.FullName -NewName $newName
|
||||||
|
|
||||||
|
# Increment counter.
|
||||||
|
$i++
|
||||||
|
}
|
||||||
0
1 - Basics/to-do-text-file-through-script.ps1
Normal file
0
1 - Basics/to-do-text-file-through-script.ps1
Normal file
Loading…
Reference in a new issue