Extending PowerShell and Docker Containers

I’ve been continuing to tinker with my PowerShell command for getting information about Docker containers. The Docker CLI is fine, but it is very difficult to work with the output or do much with it. That’s why I prefer to have objects in a PowerShell pipeline. One of the Docker container elements that I ignored until now was the container size. The whole beauty of containers is that they are small by design so why not get that information as well?

Container Size

You can use the Docker CLI to display a container size.

Display container size

From what I can tell, this is the size of writable layers in the container. For me this always seems to be 0 bytes. Still, eventually I might have a container with an actual value so I need some code to extract it.

$name = "cow1"
$sz = docker ps -asf "name=$name" --format "{{json .}}" | Convertfrom-json | Select -expandproperty size
[regex]$rx = "\d+"
$szbytes = $rx.match($sz).value

Using the JSON trick I discussed last time I can extract the Size property which in my case will be 0B. I use a regular expression pattern to get just the numeric portion of this string, or 0.

Get Docker Files

Each container also includes a set of files, such as logs, which are stored in the Docker program location. Using a little PowerShell processing, I can get that location from Docker system information.

$dockpath = ((docker system info | select-string "docker root dir") -split ": ")[1]

In Windows, this should be C:\ProgramData\Docker.  Each container has its own directory.


The folder name is the container ID which I already know how to get. From there it is a simple matter of measuring the total size of all files.

$name = "cow1"
$ID = (docker inspect $name --format "{{json .ID}}") -replace '"',''
$stat = Get-Childitem (join-path $dockpath "containers\$ID") -file -recurse | 
Measure-Object -Property length -sum

$Stat gives me a sum value of 11977.

Include Docker Container Volumes

Finally, a container might have an associated volume for persistent storage.

$c = docker inspect cow1 | Convertfrom-json
Container volumes

Again, this is pretty easy to measure.

$m = $c.mounts | foreach { get-childitem -path $_.source -file -recurse } | 
measure-object length -sum

I can then add all the values together to get a pretty close approximation of the container’s size.

Updated PowerShell Function

With this in mind, I updated the Get-DockerContainer function.

I updated the class to include size.

Get-DockerContainer function

PowerShell Formatting

The function includes code to use a format.ps1xml file which I’ve also put up on Github.

The default view is still a table.

Default view

But I also added a 2nd table view called stats.


Now I have very useful PowerShell command for at least getting Docker container information.

Docker container information

Because it is in PowerShell, I can export it, dump it to a database, create an HTML report or anything else you can think of in PowerShell. As I said, the Docker CLI is helpful, but it isn’t PowerShell useful.

About the Author:

I am long-time Microsoft professional, a multi-year Microsoft MVP in Windows PowerShell (now Cloud and DataCenter Management) and an IT veteran with almost 30 years of experience, much of it spent as an IT consultant specializing in Windows server technologies. I work today as an independent author, teacher and consultant.  I’ve co-authored or authored several books, courseware, and training videos on administrative scripting and automation. These days my focus is on Windows PowerShell, automation and DevOps. You can check out my latest books and training videos on the next page.

I hope you will follow me on Twitter . I post some of my larger projects on Github. I also invite you to join my mailing list. That will get you a daily RSS summary and occasional emails about what I’m up to such as new courses or appearances. I’m much too busy to spam you and you can opt out any time.


Hicks, J. (2019). Extending PowerShell and Docker Containers. Available at:
https://jdhitsolutions.com/blog/powershell/6629/extending-powershell-and-docker-containers/ [Accessed 16th May 2019].

Share this on...

Rate this Post: