-Exclude directories not working when using -LiteralPath in get-Childitem
Basically I want to exclude one directory from $directory.
@Anil_G Any thoughts
$directory = “C:\Users\Desktop\powershell”
$searchpattern = “. ”
$oldstring1 = ‘runAs=“India”’
$newstring1 = ‘runAs=“America”’
$oldstring2 = ‘runAsGuid=“India”’
$newstring2 = ‘runAsGuid=“America”’
$modifiedfiles = @()
Get-ChildItem -LiteralPath $directory -Include $searchpattern -Exclude *.atr -Recurse | ForEach-Object {
if($.GetType().Name -eq “FileInfo”) {
$content = Get-Content -LiteralPath $ .FullName
if($content -match $oldstring1 -or $content -match $oldstring2) {
$modifiedcontent = $content -replace [regex]::Escape($oldstring1), $newstring1 -replace [regex]::Escape($oldstring2), $newstring2
Set-Content -LiteralPath $.FullName $modifiedcontent
$modifiedfiles += $ .FullName
}
}
}
$modifiedfiles | Out-String -Stream
Anil_G
(Anil Gorthi)
May 12, 2023, 9:20am
2
@Navya_Budagam
I believe the issue is that the exclude is not going with get child …i hope the below helps…I have chnaged a bit to remove include and use filter instead
$directory = "C:\Users\Desktop\powershell"
$searchpattern = "*.*"
$oldstring1 = 'runAs="India"'
$newstring1 = 'runAs="America"'
$oldstring2 = 'runAsGuid="India"'
$newstring2 = 'runAsGuid="America"'
$modifiedfiles = @()
Get-ChildItem -LiteralPath $directory -Filter $searchpattern -Recurse | ForEach-Object {
if ($_.GetType().Name -eq "FileInfo" -and $_.Extension -ne ".atr") {
$content = Get-Content -LiteralPath $_.FullName
if ($content -match $oldstring1 -or $content -match $oldstring2) {
$modifiedcontent = $content -replace [regex]::Escape($oldstring1), $newstring1 -replace [regex]::Escape($oldstring2), $newstring2
Set-Content -LiteralPath $_.FullName -Value $modifiedcontent
$modifiedfiles += $_.FullName
}
}
}
$modifiedfiles | Out-String -Stream
Hi @Anil_G
Let me rephrase my question…
The code provided by you is working fine. I’m just making an enhancement to the code, i.e. I have multiple sub folders in $directory and I want to exclude one sub folder from $directory with the existing script.
Hope you understand…
Anil_G
(Anil Gorthi)
May 12, 2023, 5:08pm
4
@Navya_Budagam
Go you…
Would this work
$directory = "C:\Users\Desktop\powershell"
$searchpattern = "*.*"
$oldstring1 = 'runAs="India"'
$newstring1 = 'runAs="America"'
$oldstring2 = 'runAsGuid="India"'
$newstring2 = 'runAsGuid="America"'
$modifiedfiles = @()
Get-ChildItem -LiteralPath $directory -Filter $searchpattern -Recurse | ForEach-Object {
if ($_.GetType().Name -eq "FileInfo" -and $_.Extension -ne ".atr" -and $_.DirectoryName -notin @("C:\Users\Desktop\powershell\exclude-folder1", "C:\Users\Desktop\powershell\exclude-folder2")) {
$content = Get-Content -LiteralPath $_.FullName
if ($content -match $oldstring1 -or $content -match $oldstring2) {
$modifiedcontent = $content -replace [regex]::Escape($oldstring1), $newstring1 -replace [regex]::Escape($oldstring2), $newstring2
Set-Content -LiteralPath $_.FullName -Value $modifiedcontent
$modifiedfiles += $_.FullName
}
}
}
$modifiedfiles | Out-String -Stream
Cheers
1 Like
Nitya1
(Nitya Tomar)
May 12, 2023, 5:16pm
5
Hi @Navya_Budagam
To exclude a specific subfolder from a directory using PowerShell, you can utilize the -Exclude
parameter of the Get-ChildItem
cmdlet. Here’s an example of how you can modify the code to exclude a subfolder:-
$directory = “C:\Your\Directory”
$excludeFolder = “SubfolderToExclude”
$files = Get-ChildItem -Path $directory -File -Recurse -Exclude $excludeFolder
Thanks!!!
Hi @Nitya1
-path and -exclude operators works fine but -LiteralPath and -Exclude are not working in my case
Hi @Anil_G
Since we have very long paths in $directory (file lengths greater than 260 chars), I have created SUBST variable to assign $directory.
Since I’m using “Z:” next to -LiteralPath it is not excluding the directories. But if I use $directory in place of “Z:” the it is excluding the subdirectories
Below is the updated script.
Any thoughts?
$directory = “C:\Users\Desktop\powershell”
$searchpattern = “. ”
$oldstring1 = ‘runAs=“India”’
$newstring1 = ‘runAs=“America”’
$oldstring2 = ‘runAsGuid=“India”’
$newstring2 = ‘runAsGuid=“America”’
$modifiedfiles = @()
$SUBST Z: $directory
Get-ChildItem -LiteralPath “Z:” -Filter $searchpattern -Recurse | ForEach-Object {
if ($.GetType().Name -eq “FileInfo” -and $ .Extension -ne “.atr” -and $.DirectoryName -notin @(“C:\Users\Desktop\powershell\exclude-folder1”, “C:\Users\Desktop\powershell\exclude-folder2”)) {
$content = Get-Content -LiteralPath $ .FullName
if ($content -match $oldstring1 -or $content -match $oldstring2) {
$modifiedcontent = $content -replace [regex]::Escape($oldstring1), $newstring1 -replace [regex]::Escape($oldstring2), $newstring2
Set-Content -LiteralPath $.FullName -Value $modifiedcontent
$modifiedfiles += $ .FullName
}
}
}
$modifiedfiles | Out-String -Stream
@Anil_G & @Nitya1
Below worked for me…
Thank you @Anil_G
$directory = “C:\Users\Desktop\powershell”
$searchpattern = “. ”
$oldstring1 = ‘runAs=“India”’
$newstring1 = ‘runAs=“America”’
$oldstring2 = ‘runAsGuid=“India”’
$newstring2 = ‘runAsGuid=“America”’
$modifiedfiles = @()
$SUBST Z: $directory
Get-ChildItem -LiteralPath “Z:” -Filter $searchpattern -Recurse | ForEach-Object {
if ($.GetType().Name -eq “FileInfo” -and $ .Extension -ne “.atr” -and $_.DirectoryName -notin @(“Z:\exclude-folder1”, “Z:\exclude-folder2”)) {
$content = Get-Content -LiteralPath $.FullName
if ($content -match $oldstring1 -or $content -match $oldstring2) {
$modifiedcontent = $content -replace [regex]::Escape($oldstring1), $newstring1 -replace [regex]::Escape($oldstring2), $newstring2
Set-Content -LiteralPath $.FullName -Value $modifiedcontent
$modifiedfiles += $ .FullName
}
}
}
$modifiedfiles | Out-String -Stream
1 Like