Trouble with Get-ChildItem and the -Directory Parameter in PowerShell for MECM Packages
Asked by
CuriousCoder42
Hey everyone! I'm working on a PowerShell script to create multiple MECM packages based on folder names, but I'm running into an issue. Here's a snippet of my script:
```powershell
# Define paths and groups
$SourcePath = "\TKUS001INVP003Driver_Packages$WIM_PackagesLenovo"
$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 this, I keep getting an error saying: "Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'." It's strange because this syntax seems to be used everywhere. Shouldn't it work? I suspect it might be about the PowerShell version or the provider being used since I can't seem to get the correct path either. I'd love to hear if anyone has dealt with this and how you solved it!
Answers
What version of PowerShell are you using? I'm pretty sure that a network path like yours should still be handled by the FileSystem provider, so both `-Path` and `-Directory` should work. I canโt check right now, but I remember it being fine in the past. Let me know!
Reply to this answer
What does this return for you?
`Get-Item -Path $SourcePath | Select-Object PSProvider`
This could tell us what provider you're working with for that path.
Reply to this answer
Have you tried changing the double quotes in your `$SourcePath` to single quotes? It seems you might have a path issue too. I tested a UNC path with a `$` in it and it worked fine for me! Also, make sure you're running outside of PowerShell ISE if you want it to behave normally.
CuriousCoder42: I did copy the exact path from my file explorer, so it's correct. I think when I set the location to the site code in MECM, it causes the path resolution to fail. Running it in a normal session works fine though. Such a frustrating catch-22!
Reply to this answer
Can you pull the exact error from `$Error`? Iโm not seeing what you are. I tested this with both PowerShell 5.1 and 7 using a network path, and it worked just fine for me. Might give us a clue!
CuriousCoder42: Hereโs the error I'm getting: "Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'. At line:19 char:33..." Seems like the same issue persists!
Reply to this answer
Add your answer
Answers are reviewed before publication.
CuriousCoder42: Thanks for the reply! Iโm on PowerShell 5.1. I even tried using the PowerShell ISE from within MECM and still got that error. Iโll give PowerShell 7 a shot and see if it changes anything!