PowerShell BranchCache module
BranchCache cuts the WAN traffic of a branch office: content from an HQ server crosses the WAN once, is cached inside the branch, and afterwards the server sends only small hashes while the data comes from the branch LAN. The BranchCache module (32 cmdlets) configures it.
235.1 Why is it used?
- WAN links are slow and costly; many branch PCs opening the same file would pull it many times.
- With BranchCache the full data crosses the WAN only once.
- Works for SMB file shares and IIS/HTTP content.
- Enable/Disable/Set/Clear/Publish need admin; even Get-BC* can show office policy, server names and cache paths.
235.2 How it works
- Step 1
1.PC1 requests a report from the HQ file server over the WAN
- Step 2
2.Server sends only the content information (hashes)
- Step 3
3.PC1 has a cache miss: full data comes over the WAN once and is cached on PC1
- Step 4
4.PC2 requests the same file: server again sends only hashes
- Step 5
5.PC2 multicasts a WS-Discovery probe; PC1 answers "I have it"
- Step 6
6.Data goes PC1 -> switch -> PC2 on the LAN, not over the WAN
- Step 1
1.Branch server runs Enable-BCHostedServer (192.168.20.5)
- Step 2
2.Clients run Enable-BCHostedClient -ServerNames
- Step 3
3.After the WAN download, PC1 offers the data to the hosted cache server
- Step 4
4.PC2 (even on another subnet) asks the server for the Segment ID
- Step 5
5.Data comes from the hosted cache, even if PC1 is offline
- Hashes: file -> segments -> blocks; each block gets SHA-256; hash of the block hashes = Hash of Data (HoD).
- Segment secret = Hash(server secret + HoD); clients find content in the branch by the Segment ID.
235.3 Comparison: distributed vs hosted
| Point | Distributed cache | Hosted cache |
|---|---|---|
| Where the cache lives | On the client PCs of the branch | On a branch hosted cache server |
| Discovery | WS-Discovery multicast on the LAN | Clients ask the server (-ServerNames or SCP in AD) |
| Subnets | Single subnet | Multiple subnets can share |
| PC offline | Its cached content is gone | Content still available |
| Cmdlets | Enable-BCDistributed | Enable-BCHostedServer + Enable-BCHostedClient |
| Fits | Small branch, no extra hardware | Bigger branch |
235.4 What was run on Windows (read-only)
Get-Command -Module BranchCache | Measure-ObjectGet-Help Get-BCStatusPS> Get-Command -Module BranchCache | Measure-ObjectCount : 32PS> Get-Help Get-BCStatusName : Get-BCStatusModuleName : BranchCacheSynopsis :Get-BCStatus [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [<CommonParameters>]- Get-Help only shows syntax; Get-BCStatus itself was not run.
- Get-BC* cmdlets take only -AsJob, -CimSession, -ThrottleLimit; changing cmdlets also have -Confirm, -WhatIf, -Force.
235.5 Status cmdlets
Get-BCStatus # client + content server + hosted cache + networkGet-BCClientConfigurationGet-BCContentServerConfigurationGet-BCHostedCacheServerConfigurationGet-BCNetworkConfiguration235.6 Modes, on/off and tuning
Enable-BCDistributed # small branch: cache on peer PCsEnable-BCLocal # local cache onlyEnable-BCHostedServer -RegisterSCP # branch server = cache, SCP in AD DSEnable-BCHostedClient -ServerNames "HC.contoso.com"Disable-BCReset-BC # -ResetFWRulesOnly / -ResetPerfCountersOnlyEnable-BCServeOnBattery- -UseVersion on Enable-BCHostedClient: Windows8 = HTTP, Windows7 = HTTPS; -UseSCP finds servers via AD.
- ServeOnBattery: whether a laptop on battery still answers peer discovery.
Enable-BCDowngrading # -Version Windows7 | Windows8 (old V1 content info)Disable-BCDowngrading # new V2 format, more savingsSet-BCAuthentication -Mode Domain # None | Domain, on a hosted cacheSet-BCMinSMBLatency -LatencyMilliseconds 20 # default 80 ms, 0 = always cache235.7 Cache storage cmdlets
Add-BCDataCacheExtension -Path "C:\datacache" -Percentage 10Get-BCDataCacheExtensionSet-BCCache -Path "C:\datacache" -MoveTo "D:\datacache"Set-BCDataCacheEntryMaxAge -TimeDays 100Clear-BCCache- Add-BCDataCacheExtension with no parameters reserves 5% of the drive; -Percentage or -SizeBytes set the size.
- Set-BCCache also has -Defragment and -Percentage/-SizeBytes; Clear-BCCache deletes the data of all caches.
235.8 Pre-hashing, packages and secret keys
Publish-BCFileContent -Path "D:\share" -StageDataPublish-BCWebContent -Path "D:\inetpub\wwwroot" -RecurseExport-BCCachePackage -Destination "D:\temp" # docs example writes Export-BCDataPackageImport-BCCachePackage -Path "D:\temp\PeerDistPackage.zip"- Publish-BCFileContent (file shares) / Publish-BCWebContent (IIS) build hashes in advance.
- Package flow: Publish -StageData -> Export-BCCachePackage -> Import-BCCachePackage on the hosted cache, so the branch is warm before the first request.
Export-BCSecretKey -Filename "C:\share1\secretkey.key" -FilePassphrase "mySecretPhrase"Import-BCSecretKey -Filename "C:\share1\secretkey.key" -FilePassphrase "mySecretPhrase"- Cluster / load balancer: every content server needs the same secret key, otherwise each copy is cached separately.
235.9 Linux equivalent lab (nginx caching proxy)
Not BranchCache, but the same idea: HQ nginx 192.168.200.10, branch nginx proxy cache 192.168.200.20, PCs .31 and .32.
# branch.conf (n235-branch, trimmed)proxy_cache_path /var/cache/n235 keys_zone=branch:1m max_size=50m;location /files/ { proxy_pass http://192.168.200.10; proxy_cache branch; add_header X-Cache $upstream_cache_status;}# n235-pc1 first, then n235-pc2:curl -s -o /dev/null -D - http://192.168.200.20/files/report.bin | grep -iE '^HTTP|^Content-Length|^X-Cache'# n235-pc1 (192.168.200.31)HTTP/1.1 200 OKContent-Length: 1048576X-Cache: MISS# n235-pc2 (192.168.200.32)X-Cache: HITcat /var/log/nginx/branch.logcat /var/log/nginx/hq.log | cut -d' ' -f1,6-10split -b 262144 /srv/files/report.bin blksha256sum blk* | cut -c1-16,65-sha256sum blk* | wc -c192.168.200.31 "GET /files/report.bin HTTP/1.1" 200 MISS192.168.200.32 "GET /files/report.bin HTTP/1.1" 200 HIT192.168.200.20 "GET /files/report.bin HTTP/1.0" 200 1048576a256f80cbbf0d6a6 blkaa70ea5218826e753b blkaba0947c5a29beff49 blkacd722ba232c8ab509 blkad288- Branch log: 2 requests (MISS, HIT); HQ log: only 1 request, from the branch cache.
- Four block hashes are only 288 bytes of text for a 1 MB file: why only hashes need to cross the WAN.
- /proc/modules: 28 before and after, no module loaded.
235.10 Common mistakes
- Believing BranchCache means zero WAN traffic: hashes still come over the WAN and the first copy too.
- Forgetting that a changed file means new segments downloaded again from HQ.
- Different secret keys on clustered content servers.
- Choosing hosted mode for a tiny branch with no server: distributed fits better.
235.11 Interview tips
- Distributed = client PCs + WS-Discovery, single subnet; hosted = branch server, survives PC offline, multi-subnet.
- In both, the server sends only hashes; data comes from inside the branch.
- 10 laptops, no server: Enable-BCDistributed on every laptop.
- Get-BCStatus is the one-shot status cmdlet.