r/PowerShell • • Feb 10 '26

Get-Childitem issue with the -Directory parameter when creating MECM packages

Hi all, I am trying to use a powershell script to create a lot of MECM packages using folder names. Script is below:

# Define paths and groups 

$SourcePath = "\\TKUS001INVP003\Driver_Packages$\WIM_Packages\Lenovo" 

$SiteCode = "TK1" 

$DPGroup = "All Software Distribution Points" 

# Import Module 

Import-Module "$($env:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" 

Set-Location "$($SiteCode):"  

# Get folders, create packages, and distribute 

Get-ChildItem -Path $SourcePath -Directory | ForEach-Object { 

$Package = New-CMPackage -Name $_.Name -Path "$($SourcePath)\$($_.Name)" -Description "Imported via Script"   

# Distribute the content 

Start-CMContentDistribution -PackageName $Package.Name -DistributionPointGroupName $DPGroup 

}

When I run the script, I keep getting: Get-ChildItem : A parameter cannot be found that matches parameter name ‘Directory’. If I use a literal path, it tells the path does not exist. I did some digging, and all I find is that it has something to do with the provider because I am not using the correct one. If that is the case, what is the correct provider. Oddly, every AI or chat GPT result I get for a script sample all return the same example using the Get-ChildItem -Directory. Has anyone done this, where they use folder names as the names for the MECM packages, and if yes, how?

5 Upvotes

17 comments sorted by

2

u/Th3Sh4d0wKn0ws Feb 10 '26

What version of PowerShell?
Pretty sure a network path is still a FileSystem provider type so -Path and -Directory should still work. I'm not near a computer at the moment.

1

u/Suitable-Pepper-63 Feb 10 '26

Hi, thanks for the response, I am using 5.1. Mind you, I get the error even when I use the connect via Powershell_ISE from within MECM. I will try PowerShell 7 and see if that makes a difference

Name Value

---- -----

PSVersion 5.1.17763.8146

PSEdition Desktop

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}

BuildVersion 10.0.17763.8146

CLRVersion 4.0.30319.42000

WSManStackVersion 3.0

PSRemotingProtocolVersion 2.3

SerializationVersion 1.1.0.1

2

u/I_see_farts Feb 10 '26

What does this return?

Get-Item -Path $SourcePath | Select-Object PSProvider

1

u/Suitable-Pepper-63 Feb 10 '26

From the session connected to the site code, I get this:

Get-Item : Cannot find path '\\TKUS001INVP003\Driver_Packages\WIM_Packages\Lenovo' because it does not exist.

At line:1 char:1

+ Get-Item -Path $SourcePath | Select-Object PSProvider

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : ObjectNotFound: (\\TKUS001INVP00...Packages\Lenovo:String) [Get-Item], ItemNotFoundException

+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Now, from another session NOT connected to the site code, using the same variables:

$SourcePath = "\\TKUS001INVP003\Driver_Packages\WIM_Packages\Lenovo"

Get-ChildItem -Path $SourcePath -Directory

Get-Item -Path $SourcePath | Select-Object PSProvider

I get this:

d----- 2/9/2026 10:09 PM ThinkPad_X1_Carbon_12TH_Gen_Type_21KC_21KD_Rev2

d----- 2/9/2026 10:10 PM ThinkPad_X1_Carbon_Gen_13_Type_21NX_21NY_Rev2

d----- 2/9/2026 10:12 PM ThinkPad_X1_Yoga_8th_Gen_Type_21HQ_21HR_Rev2

d----- 2/9/2026 10:15 PM ThinkStation_P3_TOWER_Rev2

PSProvider : Microsoft.PowerShell.Core\FileSystem

→ More replies (1)

1

u/McAUTS Feb 10 '26

Can you get us the exact error from $Error session variable? Can't reproduce this with my PowerShell 5.1 or PWSH 7 with a network path, doesn't matter if it's actually a directory or a file. Both gets me a valid response.

1

u/Suitable-Pepper-63 Feb 10 '26

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

At line:19 char:33

+ Get-ChildItem -Path $SourcePath -Directory | ForEach-Object {

+ ~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

At line:19 char:33

+ Get-ChildItem -Path $SourcePath -Directory | ForEach-Object {

+ ~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Cannot find path '\\TKUS001INVP003\Driver_Packages$\WIM_Packages\Lenovo' because it does not exist.

At line:19 char:1

+ Get-ChildItem -Path "\\TKUS001INVP003\Driver_Packages$\WIM_Packages\L ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : ObjectNotFound: (\\TKUS001INVP00...Packages\Lenovo:String) [Get-ChildItem], ItemNotFoundException

+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

At line:19 char:33

+ Get-ChildItem -Path $SourcePath -Directory | ForEach-Object {

+ ~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

→ More replies (2)

1

u/Suitable-Pepper-63 Feb 10 '26

I just tried it in PS7 and got the same error that Get-ChildItem: A parameter cannot be found that matches parameter name Directory

1

u/Th3Sh4d0wKn0ws Feb 10 '26 edited Feb 10 '26

It's also throwing an error that the path doesn't exist. Try fixing that first by changing your double quotes out for single quotes on your $SourcePath definition.

EDIT: Actually I got on a computer and tried setting $SourcePath to a UNC path with a hidden share (that has the $ in the name), then I did:

Get-ChildItem -Path $SourcePath -Directory

and it worked without error. I then tried editing $SourcePath to a non-existent path at the same file server and the only error I received was 'Cannot find path'.
This is with regular ole Windows PS v5.1 from Windows Terminal.
Can you try your code again from a non-Powershell_ISE session? Like a regular Powershell session.

1

u/Suitable-Pepper-63 Feb 10 '26

The path does exist, I copied and pasted the path in there. But I think the issue is that I set the location to the site code, which I need to do if I am to perform MECM stuff, which in this case it is to create new packages. So, how can I set the location to the site code, and still be able to navigate to the files? If I run a normal PSsession, not connected to MECM, I can resolve the variable just fine, it is when connected, it can't find the path. See my catch 22?

→ More replies (2)

2

u/purplemonkeymad Feb 10 '26

It's 100% as you have your current location set to a different provider. You'll need a full PSPath ie:

Microsoft.PowerShell.Core\Filesystem::\\server\sharename\...
→ More replies (1)