I have a PowerShell cmdlet with the following line
$items = Get-ChildItem -Path $FolderName -File -Force |
Sort CreationTime |
Select -First 1 -Last 1
It works fine under my normal login but if I log onto my machine as a domain admin I get an error message telling me that -File
is not recognised as a valid parameter for Get-ChildItem
.
I suspected that the domain admin was running an earlier version of PowerShell so under both accounts I have run $PSVersionTable.PSVersion
and get the following:
Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 117
If anything I would expect my local login to fail and the domain admin login to succeed due to permissions differences but it seems to be working the other way around.
Could Get-ChildItem somehow have been overwritten in your $profile or something else?
You can check what Get-ChildItem executes if you run this:
get-command Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-ChildItem 3.1.0.0 Microsoft.PowerShell.Management
If it is overwritten by doing something like this:
Function Get-ChildItem { }
Then it will would show this:
get-command Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Function Get-ChildItem
If that would be the case, you can remove the custom version with
Remove-Item Function:\Get-ChildItem
You could also try to not use the -File parameter, and rather filter out folders yourself:
$items = Get-ChildItem -Path $FolderName -Force |
Where PSIsContainer -eq $False |
Sort CreationTime |
Select -First 1 -Last 1