How do I format a number cell to Accounting in Excel using Powershell?

Posted on 2010-09-23
Last Modified: 2012-05-10
I have a Powershell script that creates an Excel spreadsheet. I would like to format some of the cells with numbers to Accounting with 0 decimal places and no symbol. How do I do this? For example, how would I change the format to Accounting for the numbers in column 2 on rows 7-12 in my snippet code below?
$Excel = New-Object -Com Excel.Application

$Excel.visible = $False

$Excel.DisplayAlerts = $False

$Wkbk = $Excel.Workbooks.Add()

$Sheet = $Wkbk.Worksheets.Item(1)

$Sheet.Cells.Item(1,1) = "ID Admin"

$Sheet.Cells.Item(2,1) = "Outstanding Task Summary"

$Sheet.Cells.Item(3,1) = "Date: " + $Date

$Sheet.Cells.Item(7,2) = $disableuser_1days

$Sheet.Cells.Item(8,2) = $disableuser_2days

$Sheet.Cells.Item(9,2) = $disableuser_3days

$Sheet.Cells.Item(10,2) = $disableuser_4days

$Sheet.Cells.Item(11,2) = $disableuser_5days

$Sheet.Cells.Item(12,2) = $disableuser_gt5days

$Range = $Sheet.Range("A1:H1")

$Range.MergeCells = $true

$Range = $Sheet.Range("A2:H2")

$Range.MergeCells = $true

$Range = $Sheet.Range("A3:H3")

$Range.MergeCells = $true

$intRow = 7

	Do {

	$Sheet.Cells.Item($intRow,2).HorizontalAlignment = -4108

	$Sheet.Cells.Item($intRow,2).VerticalAlignment = -4160


	} Until ($intRow -gt 12)

$WorkBook = $Sheet.UsedRange




Open in new window

Question by:reason4xistence
LVL 85

Accepted Solution

Rory Archibald earned 500 total points
ID: 33744350
I suspect:
$Sheet.Range("B7:B12").NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""_);_(@_)"

Author Closing Comment

ID: 33744823
Wow! I am going to look up NumberFormat. That doesn't look like normal Powershell regular expressions. At any rate, it works perfectly. Thanks so much, rorya!

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now