# Old code stopped working in the past week

**URL:** <https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142>\
**Category:** PowerShell Help\
**Created:** [July 22, 2021, 10:47pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142 "2021-07-22T22:47:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 22, 2021, 10:47pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/1 "2021-07-22T22:47:33Z")

</div>

I’m using Powershell to conduct migrations of mass files from a network drive to a user’s OneDrive. My Powershell has been working for the past several months, until this past week. My code steps through all the user’s files and produces a report that lists all the files older than 3 years. A user that has a HUNDREDS of files going back to 2012 called me, asking why his report of old files was empty. Looking through the code I found that the following code fails to pull any files, except for the files in the root folder.

```auto
$dir = <the user's root folder of their network drive>

# The following is a list of folders in the root folder that we don't want to move to OneDrive.
$ExcludeDir = "Adobe|CSF|Favorites|IBM|Outlook|Remote|Windows|Remote Assistance Logs|Google|My Data Sources|Datair|Downloads|My Shapes|My Videos|My Pictures|My Music|Recycler|cache"

Get-ChildItem -File -Path $dir -Recurse | Where-Object {$_.PSChildName -notmatch $ExcludeDir -and $_.PSParentPath -eq "Microsoft.PowerShell.Core\FileSystem::" + $dir}

```

the above only pulls up the files in the user’s root folder.

```auto
Get-ChildItem -File -Path $dir -Recurse | Where-Object {$_.PSChildName -notmatch $ExcludeDir}

```

The above pulls a near full list of files. None of the files from the folders listed in $ExcludeDir

```auto
Get-ChildItem -File -Path $dir -Recurse  

```

The above pulls a nearly the same list of files. The test user I’m using, the list only includes 10 extra lines in the output… There should be dozens, if not over a hundred lines. None of the additional files are from the “ExcludeDir” list. They are from the list of folders I had previously pulled.

The difference in files also doesn’t make sense.

Example file name: Interest Rate Outlook.xls

I’m completely perplexed how this stopped working all of a sudden. Thoughts?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [July 22, 2021, 11:13pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/2 "2021-07-22T23:13:53Z")

</div>

Personally, I would have done things differently, but I actually have a question on your declaration of:

```auto
$ExcludeDir

```

I am not at all familiar with that syntax. You have spaces, pipes and no quotes at all. I had no idea PowerShell could deal with that. Can you explain for my curiosity? Thanks.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 22, 2021, 11:59pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/3 "2021-07-22T23:59:40Z")

</div>

> [@tonyd](#):
>
> I am not at all familiar with that syntax.

I’m pretty sure the code is incomplete/broken. … happened probably while pasting here.  
`$ExcludeDir` is most likely a regex pattern string and should actually be wrapped in quotes. 😉  
And there is a pipe missing right in front of `Where-Object` …

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [July 23, 2021, 12:05am UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/4 "2021-07-23T00:05:12Z")

</div>

Good to know, thanks Olaf 🙂

---

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 23, 2021, 12:26am UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/5 "2021-07-23T00:26:45Z")

</div>

You’re completely correct on both observations. There is a missing pipe, I’ll add that back in.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 12:45am UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/6 "2021-07-23T00:45:27Z")

</div>

> [@wenin](#):
>
> `$_.PSChildName -notmatch $ExcludeDir`

With this code you check if the file name - and it’s really just the file name without the path - does not match to any of the partly strings you have in your regex pattern. If you want to explude folders that does not make that much sense.

I’d expect you check against either the fullname of the file or the directory name.

> [@wenin](#):
>
> `$_.PSParentPath -eq "Microsoft.PowerShell.Core\FileSystem::" + $dir`

Since you obviously want to make sure to have only files contained in the folder the user specified why don’t you start your `Get-ChildItem` command in this folder?

---

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 23, 2021, 3:02am UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/7 "2021-07-23T03:02:23Z")

</div>

I’m now seeing all the causes of the issues. This code could never have worked… that’s not good for the previous 4 groups I migrated…

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 6:21am UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/8 "2021-07-23T06:21:40Z")

</div>

> [@wenin](#):
>
> This code could never have worked… that’s not good for the previous 4 groups I migrated…

I’m sorry for you. 😳

> [@wenin](#):
>
> I’m now seeing all the causes of the issues.

That’s something positive I think. 👍🏽 🙂

No you can start over and do it right. 😉

---

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 23, 2021, 4:01pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/9 "2021-07-23T16:01:41Z")

</div>

Issue is I don’t have time to start over. =)

Also I checked my log files from earlier in the year and the script was working… somehow…

Any recommendations on how I could adjust the Get-ChildItem call to exclude all files from the list of folders? That’s my main issue.

I’m still confused how the raw Get-ChildItem call doesn’t appear to be pulling files from those folders… Going to go back and look closer.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [July 23, 2021, 6:16pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/10 "2021-07-23T18:16:12Z")

</div>

I was going to suggest filtering the folders first to make it quicker but using an exclude list is problematic because when `-recurse` is used it still includes files in subfolders of excluded folders and it excludes folders with the same name, in folders that you want to include. e.g. if you exclude **`folder1`** then  
`E:\Temp\psFiles\folder1\subfolder1\file1.txt` gets included but  
`E:\Temp\psFiles\folder2\folder1\file1.txt` is excluded.

I think I would take the approach of getting all the files then splitting on the path to ensure that the folder you’re excluding is in the root folder:

```auto
$dir = 'E:\Temp\psFiles'
$excludeList = 'folder1','folder5'
Get-ChildItem -File -Recurse | Where-Object {($_.FullName -split '\\')[3] -notin $excludeList} | Select-Object FullName

```

**Output**

```auto
FullName
--------
E:\Temp\psFiles\folder2\folder2-file1.txt
E:\Temp\psFiles\folder2\folder2-file2.txt
E:\Temp\psFiles\folder2\folder1\folder2-folder1-file1.txt
E:\Temp\psFiles\folder3\folder3-file1.txt
E:\Temp\psFiles\folder3\folder3-file2.txt
E:\Temp\psFiles\folder4\folder4-file1.txt
E:\Temp\psFiles\folder4\folder4-file2.txt
E:\Temp\psFiles\folder6\folder6-file1.txt
E:\Temp\psFiles\folder6\folder6-file2.txt

```

---

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 23, 2021, 8:18pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/11 "2021-07-23T20:18:59Z")

</div>

> [@matt-bloomfield](#):
>
> ```auto
> $dir = 'E:\Temp\psFiles'
> $excludeList = 'folder1','folder5'
> Get-ChildItem -File -Recurse | Where-Object {($_.FullName -split '\\')[3] -notin $excludeList} | Select-Object FullName
> 
> ```

I’m still getting files within those folders.

```auto
$ExcludeDir = 'Adobe','CSF','Favorites','IBM','Outlook','Remote','Windows','Remote Assistance Logs','Google','My Data Sources','Datair','Downloads','My Shapes','My Videos','My Pictures','My Music','Recycler','cache'
$files = Get-ChildItem -File -Path $dir -Recurse | Where-Object {($_.FullName -split '\\')[3] -notin $ExcludeDir} | Select-Object FullName

```

||Line 3707: \egfiler1\user\_sata\<username\>\Favorites\Bing.url  
||Line 3708: \egfiler1\user\_sata\<username\>\Favorites\FabVirtualHome.url

I’ll look more into how -split works to figure out why it isn’t doing what you thought it would.

The edits are from me swapping in and out double quotes and single quotes for the code I ran, to see if there was a difference. Mirroring your suggestion, using single quotes, found no difference.

---

<div class="post-metadata">

**Author:** ![wenin](https://avatars.discourse-cdn.com/v4/letter/w/779978/32.png) [@wenin](https://forums.powershell.org/u/wenin)\
**Post date:** [July 23, 2021, 8:27pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/12 "2021-07-23T20:27:28Z")

</div>

> [@wenin](#):
>
> `$_.FullName -split '\\'`

Ah I see the issue! Need to change the [3] to the correct value in the split out array!

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [July 23, 2021, 8:34pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/13 "2021-07-23T20:34:09Z")

</div>

> [@wenin](#):
>
> Ah I see the issue! Need to change the [3] to the correct value in the split out array!

You do. Sorry, I should have mentioned that. Well done for figuring it out 👍. Are you getting the expected results now?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:23pm UTC](https://forums.powershell.org/t/old-code-stopped-working-in-the-past-week/17142/14 "2024-05-16T20:23:48Z")

</div>


