Reputation: 11
I am trying to write Get-foldersize Function but I am not able to arrange the complete output. it is ended up with dots.
Here is the Script details.
Function Get-FolderSize
{
[CmdletBinding(DefaultParameterSetName='FolderPath')]
param
(
[Parameter(Mandatory=$true,Position=0,ParameterSetName='FolderPath')]
[String[]]$FolderPath,
[Parameter(Mandatory=$false,Position=1,ParameterSetName='FolderPath')]
[String]$graterthan,
[Parameter(Mandatory=$false,Position=2,ParameterSetName='FolderPath')]
[switch]$Recurse
)
Begin
{
#$graterthan and $ZeroSizeFolders cannot be used together
#Convert the size specified by Greaterhan parameter to Bytes
$size = 1000000000 * $graterthan
}
Process {#Check whether user has access to the folders.
Try {
Write-Host "Performing initial tasks, please wait... " -ForegroundColor Magenta
$subfolders = If ($Recurse) {Get-ChildItem $FolderPath -Recurse -ErrorAction SilentlyContinue }
Else {Get-ChildItem $FolderPath -ErrorAction SilentlyContinue }
}
Catch [exception]{}
#Calculate folder size
If ($subfolders)
{
Write-Host "Calculating size of folders in $FolderPath. This may take sometime, please wait... " -ForegroundColor Magenta
$Items = $subfolders | Where-Object {$_.PSIsContainer -eq $TRUE -and `
@(Get-ChildItem -LiteralPath $_.Fullname -Recurse -ErrorAction SilentlyContinue | Where-Object {!$_.PSIsContainer}).Length -gt '0'}}
ForEach ($i in $Items)
{
$subFolders =
If ($graterthan)
{Get-ChildItem -Path $i.FullName -Recurse | Measure-Object -sum Length | Where-Object {$_.Sum -ge $size -and $_.Sum -gt 1000000 } }
Else
{Get-ChildItem -Path $i.FullName -Recurse | Measure-Object -sum Length | Where-Object {$_.Sum -gt 1000000 }}
#Return only values not equal to 0
ForEach ($subFolder in $subFolders) {
#If folder is less than or equal to 1GB, display in MB, If above 1GB, display in GB
$si = If (($subFolder.Sum -ge 1000000000) ) {"{0:N2}" -f ($subFolder.Sum / 1GB) + " GB"}
ElseIf (($subFolder.Sum -lt 1000000000) ) {"{0:N0}" -f ($subFolder.Sum / 1MB) + " MB"}
$Object = New-Object psobject -pro @{
'Folder Name' = $i.Name
'Size' = $si
'Full Path' = $i.FullName
}
[array]$space = $Object
$Object | Select-Object 'Folder Name', 'Full Path',Size
}
}
}
End {
Write-Host "Task completed...if nothing is displayed:
you may not have access to the path specified or
all folders are less than 1 MB" -ForegroundColor Cyan
}
}
OUTPUT:
Name Created **FilePath** SizeMB
---- ------- -------- ------
Microsoft Analysis Services 3/13/2017 11:31:16 PM C:\Program Files\Microsoft An... 102.14
Microsoft DNX 3/16/2017 3:17:02 PM C:\Program Files\Microsoft DNX 0.08
Microsoft Help Viewer 3/20/2017 3:31:30 PM C:\Program Files\Microsoft He... 55.89
Microsoft IT Diagnostics U... 3/17/2017 5:36:08 PM C:\Program Files\Microsoft IT... 0.09
I am trying to get complete file path. even I tried out-string -width 300
and tried to out-file. When I tried out-string it is displaying complete filepath but the objects printing every time before the new output line.
Like this
Name Created FilePath SizeMB
---- ------- -------- ------
Program Files (x86) 9/29/2017 6:46:33 AM C:\Program Files (x86) 10616.26
Name Created FilePath SizeMB
---- ------- -------- ------
TURBOC3 12/2/2017 4:57:56 AM C:\TURBOC3 1
Please can someone suggest the right way to print complete filepath and all output without dots. Thanks in advance :)
Upvotes: 1
Views: 489
Reputation: 47792
To be honest, this isn't for you to worry about. The data is not missing, it's just being truncated for display purposes.
$data = Get-FolderSize
$data.FilePath
When you look at the property directly, it's there.
An end user will decide when they want to see the full output, or format it as necessary:
$data | Format-Table -AutoSize
$data | Format-List
If you really want your command's output to be displayed specially, you could create a custom format specification, but this will also require you to create a new type to apply that format to.
For your example, I really don't think it's worth the effort. People who use PowerShell tend to quickly get used to the idea of objects being returned, and how to use and manipulate those objects for display.
Do not use Out-String
in your function; by doing so you are destroying the original object and turning the output into pure text.
Upvotes: 1