Want to add date to SQL results as static column, in Powershell

so I have the following powershell script, the script works however i would like to add a column to the front of the table that is simply 'date' and every line would have '$dMonth' in the beginning of the csv.

$dMonth= (Get-Date).AddMonths(-0).ToString("dd-MM-yyyy")
$DBServer = "cdiserv1"
$databasename = "SUBSCRIBER"
$Connection = new-object system.data.sqlclient.sqlconnection
$Connection.ConnectionString ="server=$DBServer;database=$databasename;trusted_connection=True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlQuery = @"
SELECT  isnull(B.AcctLineCode, '') AS OLC,
        isnull(B.AcctNum, '') AS OAN,
        isnull(A.AcctLineCode, '') as NLC,
        isnull(A.AcctNum, '') AS NAN,
        isnull(B.Name, '') AS ONA,
        isnull(A.Name, '') as NNA,
        isnull(B.UserField1, '') AS O1,
        isnull(B.UserField2, '') AS O2,
        isnull(B.UserField3, '') AS O3,
        isnull(A.UserField1, '') AS N1,
        isnull(A.UserField2, '') AS N2,
        isnull(A.UserField3, '') AS N3,
        case when A.AcctNum != B.AcctNum or A.AcctLineCode != B.AcctLineCode then 'ACCOUNT MOVED'
        when A.Name != B.Name then 'NAME CHANGE'
        when A.UserField1 != B.UserField1 OR A.UserField2 != B.UserField2 OR A.UserField3 != B.UserField3 then 'FEATURE CHANGE'
        when A.AccountID is not null and B.AccountID is null then 'ADDED ACCOUNT' 
        when A.AccountID is null and B.AccountID is not null then 'DEELETED ACCOUNT' end [Result]
FROM    [CDISERV1].[SUBSCRIBER].[dbo].[Subscriber Data] A
        FULL OUTER JOIN [CDISERV2].[SUBSCRIBER].[dbo].[Subscriber Data] B ON A.AccountID = B.AccountID
WHERE (A.AccountID is not null and B.AccountID is null)
OR (A.AccountID is null and B.AccountID is not null)
OR A.AcctLineCode != B.AcctLineCode
OR A.AcctNum != B.AcctNum
OR A.Name != B.Name
OR A.UserField1 != B.UserField1
OR A.UserField2 != B.UserField2
OR A.UserField3 != B.UserField3
$SqlCmd.CommandText = $SqlQuery
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlCmd.Connection = $Connection
$dt = New-Object System.Data.DataTable
$SqlAdapter.Fill($dt) | Out-Null
$dt | Export-Csv -Path Z:\OP\CSV\AIS$dMonth.CSV -force -NoTypeInformation

Open in new window

i really wanted to have the script merge into a single file, as i run this script every evening just before midnight, if it would append the date would show when the data was extrapolated essentially as a log, i have discovered sadly this is not possible for Export-Csv with the version of powershell i am using so i merge the files after exporting.

my desired results would look like the following...
"15-05-2018","xxx","1234","xxx","1233","na","na","c7","n3","b5"," "," "," ","ACCOUNT MOVED"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Export-Csv may not support it, but Select-Object and its "calculated properties" does.
Just replace the last line in your script with these two:
$date = Get-Date -Format 'dd-MM-yyyy'
$dt | Select-Object -Property @{n='Date'; e={$date}}, * | Export-Csv -Path Z:\OP\CSV\AIS$dMonth.CSV -force -NoTypeInformation

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
csePixelatedAuthor Commented:
Wonderful, thank you oBdA
csePixelatedAuthor Commented:
I know i have closed this, but i now have 3 aditional colums other than the date, at the end of the table i have, Table, ItemAray, & HasErrors

how do i exclude these columns?
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

csePixelatedAuthor Commented:
actually its 5 additional columns, i forgot RowErrror & RowState...
I'm really tempted to just loudly ignore this, since you already gave the answer yourself ;)
Select-Object has an -ExcludeProperty argument (and exclude would work just as well)
Anyway, the last line(s) would need to be
$dt | Select-Object -Property @{n='Date'; e={$date}}, * -ExcludeProperty  Table, ItemAray, HasErrors, RowErrror, RowState |
	Export-Csv -Path Z:\OP\CSV\AIS$dMonth.CSV -force -NoTypeInformation

Open in new window

csePixelatedAuthor Commented:
Once again, thank you oBdA
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.