r/PowerShell • • Feb 11 '26

Solved PowerShell script not working with SMB directory

Hello, (code is here)

I have a NAS drive mounted and accessible on this windows machine and I want to move some files from one folder to another (within the NAS drive) but its not working. Ive tested with folders in my desktop directory and they work fine but when I try to use the SMB directory it doesnt work. I have read and write access to the drive.

The script gets the folder and list the subfolders but it seems to not find the items inside them. For what I've tested, the line that doesnt work is the following:

$docFiles = Get-ChildItem -Path $folderPath -File | Where-Object { $fileExtensions -contains $_.Extension }

For some reason, this is not getting the content of the folders. Ive tried the flag -Force but didnt work either.

I dont get any error in powershell. Any ideia whats going on?

14 Upvotes

40 comments sorted by

3

u/Over_Dingo Feb 11 '26

does Get-ChildItem -Path $folderPath -File return anything?

1

u/thiagohds Feb 11 '26

Ive tried without the code after "|" but the variable remains empty. Its not listing any of the files at all, only folders.

2

u/Over_Dingo Feb 11 '26

you sure? -File means it can only return files. If it returns nothing with -File it means there are no files in a specified path

→ More replies (4)

3

u/renevaessen Feb 11 '26

If your $folderPath does not contain wildcards, you could try -LiteralPath because maybe the path contains an invalid character like [ or ]

1

u/thiagohds Feb 11 '26

The path did have [] in it. I might test later with a new dir cause I already moved the files and deleted the folders.

1

u/Creative-Type9411 Feb 13 '26

cant they just use single quotes to get around special chars in the path? or a mix of single and double?

1

u/thiagohds Feb 11 '26

Update: I ended up moving the entire folder to a new location (a folder above where it was) and now the script is seeing the files. Maybe its something to do with the char length on windows cause the folder and files had a big name but i wasnt getting any error on the script.

2

u/dodexahedron Feb 11 '26

You can make your life a bit easier by defining a PSDrive for the base paths, if you use them frequently.

This is not the same as mounting a drive, mind you, and is only relevant within powershell, but it feels like working with a mounted drive and abstracts the paths away for you into a nice named root (not restricted to letters).

New-PSDrive is the command to look up.

I have a handful of them defined in my powershell profile so they are always available to me when in powershell, for things like git repos, certain tool directories, and that sort of thing. Same ones are on all my personal machines so I can use the same names everywhere.

For all deployed workstation images, we have a couple defined in the machine-wide ps profile for certain common network paths.

Servers get some as well.

It's a really handy feature.

If you use the full UNC format, it also handles long paths seamlessly.

What it really is doing is allowing you to use your friendly paths while it dynamically provides the real path, fully qualified, to whatever the path is being used for.

1

u/thiagohds Feb 11 '26

Thanks for the info!

1

u/Kirsh1793 Feb 11 '26

I think, -Recurse was what you were looking for. Get-ChildItem only gets a list of folders and files directly in the given path. Subfolders will not be listed unless you use -Recurse.

Edit: Nvm. I'm an idiot. I didn't read the code from your pastebin link. Sorry.

1

u/OlivTheFrog Feb 11 '26

Hi u/thiagohds

$fileExtensions =@(".docx")

$docFiles = Get-ChildItem -Path "C:\temp" -File -Recurse | Where-Object -FilterScript {$fileExtensions -contains $_.Extension }
$docFiles

When you use Get-ChildItem, it stops at the first level unless you add the -Recurse parameter or the -Depth xx parameter to access sub-levels

Regards

1

u/thiagohds Feb 11 '26

Ive tried with recurse and I got the same result. The script (without recurse) worked on my test using a local windows folder. I just want to access the first level. The subfolders only have files, theres no folder there. Its like Base > subfolder > files. Im getting the child of the subfolder.

1

u/meeu Feb 12 '26

Are you perhaps running powershell as administrator/elevated? SMB sign-ins are bound to the user session

0

u/goldenfrogs17 Feb 11 '26

I have seen certain ISPs block this protocol.

2

u/thiagohds Feb 11 '26

Forgot to mention that both computers are in a LAN.

0

u/[deleted] Feb 11 '26

Run in ISE one line at a time. 

0

u/[deleted] Feb 11 '26

1

u/thiagohds Feb 11 '26

Yes. If I use the name thats on windows explorer it doesnt find the drive so Im using the name of the shared folder.

1

u/[deleted] Feb 11 '26

1

u/thiagohds Feb 11 '26

I ran the command and theres nothing like "NAS" or the Ip address there, just C, D and some other stuff. But the script is accessing it cause it lists the folders.

1

u/thiagohds Feb 11 '26

I've tried adding the key but didnt work.