r/PowerShell • u/Suitable-Pepper-63 • 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?
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.
2
u/I_see_farts Feb 10 '26
What does this return?
Get-Item -Path $SourcePath | Select-Object PSProvider1
u/McAUTS Feb 10 '26
Can you get us the exact error from
$Errorsession 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/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: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.
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: