PowerShell Important FileSystem Commands

WINDOW: List file names

Get-ChildItem . -File -Recurse -Filter *123213*

WINDOW: List directory names only

Get-ChildItem . -Dir -Recurse -Filter *123213*

WINDOW: List file names and directory names only

Get-ChildItem . -Recurse -Filter *123213*

WINDOW: For change in file names and directory names only

Get-ChildItem . -Recurse -Filter *new* |Rename-Item -NewName {$_.Name.Replace("new", "old")}

WINDOW: For change in file names only

Get-ChildItem . -File -Recurse -Filter *new* |Rename-Item -NewName {$_.Name.Replace("new", "old")}

WINDOW: For change in file names and directory names only

Get-ChildItem . -Dir -Recurse -Filter *Hello* |Rename-Item -NewName {$_.Name.Replace("Hello", "erererererer")}

Find match sting in dir recursively

Get-ChildItem -Recurse | Select-String "asdfsdfsdfsdfsdf" -List | Select Path

Window search recursively : case insensitive

dir -Recurse | Select-String -pattern "string"

Window search recursively : case sensitive

dir -Recurse | Select-String -CaseSensitive -pattern "String"

==================================================

Replace string recursively Case-sensitive : window

$files = Get-ChildItem -File -Recurse

$find = 'oldtext'

$replace = 'newtext'
    Get-ChildItem $files -Recurse |
    select -ExpandProperty fullname |
    foreach {
    If(Select-String -Path $_ -CaseSensitive -SimpleMatch $find -quiet){
    (Get-Content $_) |
    ForEach-Object {$_ -replace $find, $replace } |
    Set-Content $_
    write-host "File Changed : " $_
           }
      }

==================================================

Replace string recursively Case InSensitive : window

$files = Get-ChildItem -File -Recurse

$find = 'oldtext'

$replace = 'newtext'
    Get-ChildItem $files -Recurse |
    select -ExpandProperty fullname |
    foreach {
    If(Select-String -Path $_ -SimpleMatch $find -quiet){
    (Get-Content $_) |
    ForEach-Object {$_ -replace $find, $replace } |
    Set-Content $_
    write-host "File Changed : " $_
           }
      }

SET ENVIRONMENT VARIABLE: by CMD administrative

setx /M PATH "%PATH%;C:\Program Files\php-7.4.33-nts-Win32-vc15-x64"

Reference Links:-
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.3
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-7.3

Leave a comment

Your email address will not be published. Required fields are marked *