Hello,
When I run following code from the shell, it indeed gives me a .csv with all files bigger than 1MB in c:\test
Get-ChildItem -Path c:\test -Recurse | Where-Object -FilterScript {$_.Length -ge 1MB} | Select-Object Name,@{Label='SizeMB';Expression={($_.Length/1MB)}},Directory,Extension,LastWriteTime,LastAccessTime | Export-Csv -Path export.csv
To make the code a bit more re-usable, I am trying to make a cmdlet out of it with a few variables
function Get-BiggestFiles { Param ( [Parameter(Mandatory=$true)]$SearchPath, [Parameter(Mandatory=$true)]$MinSize, [Parameter(Mandatory=$true)]$ExportName ) Get-ChildItem -Path $SearchPath -Recurse | Where-Object -FilterScript {$_.Length -ge $MinSize} | Select-Object Name,@{Label='Size(MB)';Expression={($_.Length/1MB)}},Directory,Extension,LastWriteTime,LastAccessTime | Export-Csv -Path $ExportName}
function Get-BiggestFiles {
Param ( [Parameter(Mandatory=$true)]$SearchPath, [Parameter(Mandatory=$true)]$MinSize, [Parameter(Mandatory=$true)]$ExportName )
Get-ChildItem -Path $SearchPath -Recurse | Where-Object -FilterScript {$_.Length -ge $MinSize} | Select-Object Name,@{Label='Size(MB)';Expression={($_.Length/1MB)}},Directory,Extension,LastWriteTime,LastAccessTime | Export-Csv -Path $ExportName}
Now, while this works with entering the size in bytes, it does not work when using the 1MB notation. It will throw me an error like this:
Bad argument to operator '-ge': Could not compare "110766" to "1MB". Error: "Cannot convert value "1MB" to type "System.Int64". Error: "Input string was not in a correct format."
I have tried to have the variable $MinSize first converted into an integer, but that doesn't seem to help (or the way I am trying to do it is wrong).
Can someone get me on the right track?
Thanks!
Try this.function Get-BiggestFiles {Param ( [Parameter(Mandatory=$true)]$SearchPath, [Parameter(Mandatory=$true)]$MinSize, [Parameter(Mandatory=$true)]$ExportName )Try{$MinSize = [Convert]::ToInt64($MinSize)}Catch{"Could not covert to int64";$error[-1]} Get-ChildItem -Path $SearchPath -Recurse | Where-Object -FilterScript {$_.Length -ge $MinSize} | Select-Object Name,@{Label='Size(MB)';Expression={($_.Length/1MB)}},Directory,Extension,LastWriteTime,LastAccessTime | Export-Csv -Path $ExportName}
How exactly you called the function?
@freakling: Tried your suggestion, but the error remains
@nohandle: This function is just in a custom module (it already contains working functions) which I load through my Powershell profile. Then I just call it from the commandline.