I am importing a csv to an excel using powershell. Both of my columns have date-time in the following format:
2019-01-25T19:58:28.000Z
I want to convert the column to the following format "dd/MM/yyyy h:mm"
I tried the following two things and none of them worked:
[datetime]::ParseExact($fullinfo.A, "dd/MM/yyyy h:mm", $null)
$excel.Columns.item('b').NumberFormat = "dd/MM/yyyy h:mm"
$fullInfoSheet = $workbook.Worksheets.Item(1)
$fullInfoSheet.cells.item(1,1) = "Column A"
$fullInfoSheet.cells.item(1,2) = "Column B"
$fullinfolist = Import-Csv -Path $csvfullFile -Header A, B
$i = 2
foreach($fullinfo in $fullinfolist)
{
$fullInfoSheet.cells.item($i,1) = [datetime]::ParseExact($fullinfo.A, "dd/MM/yyyy h:mm", $null)
$fullInfoSheet.cells.item($i,2) = $fullinfo.B
$i++
}
$excel.Columns.item('b').NumberFormat = "dd/MM/yyyy h:mm"
This produces output in your desired format
$InputDate = '2019-01-25T19:58:28.000Z'
get-date $InputDate -Format 'dd/MM/yyyy h:mm'
Related
$var = "01/01/2020"
$date1 = Get-Date
$date2 = Get-Date
Write-Host $date1.GetType().Name
Write-Host $date2.GetType().Name
$dates = #(
#('date1', [ref]$date1),
#('date2', [ref]$date2)
)
foreach($date in $dates) {
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null)) -Format "dd/MM/yyyy"
}
Write-Host $date1
Write-Host $date2
Write-Host $date1.GetType().Name
Write-Host $date2.GetType().Name
output :
DateTime
DateTime
01/01/2020
01/01/2020
String
String
I do not understand why my dates (date1 and date2) went from DateTime to String ?
How do I fix it ? because the next step in my code is to compare date1 and date2 (I want to compare dates not string obviously)
Help is much appreciate
This is the problem
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null)) -Format "dd/MM/yyyy"
Remove the -Format "dd/MM/yyyy" from your code.
You are parsing a string to a date using [DateTime]::ParseExact then immediately convert it back to a string using -Format "dd/MM/yyyy".
You just need
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null))
I am looking to move daily created files each day to another folder. These files are saved to the relevant YYYY\MM\folder each day. Now I have created a way to move these files over using the year/month date function, however because there a number attached to the month, i.e. December looks like "12. December" it becomes a little tricky.
I tried to amend this with an If statement which would assign "a" to the relevant number corresponding with the month however it doesnt work.
$year = (Get-Date).Year
$month = Get-Date -Format "MMMMMMMM"
$day = (Get-Date).Day
$a = ""
If ($month = "January") { $a = "1."}
Elseif ($month = "February") { $a = "2."}
Elseif ($month = "March") { $a = "3."}
Elseif ($month = "April") { $a = "4."}
Elseif ($month = "May") { $a = "5."}
Elseif ($month = "June") { $a = "6."}
Elseif ($month = "July") { $a = "7."}
Elseif ($month = "August") { $a = "8."}
Elseif ($month = "September") { $a = "9."}
Elseif ($month = "October") { $a = "10."}
Elseif ($month = "November") { $a = "11."}
Elseif ($month = "December") { $a = "12."}
$month = Get-Date -Format $a" MMMMMMMM"
Copy-Item -Path F:\BB\$year\$month\Scan.pdf -Destination F:\BB
Any idea how to fix this/where am i going wrong. This is my first time writing in Powershell.
Edit: I am getting an error in the file location it is copying to does not register the difference in the coressponding months. For example the if statement states that if the month is = December a should = 12. but its currently coming up as 1. which should be the case for if it were January
The different forms of the month may as well be repeated in the format string, where
M = month number without leading zeroes
MM = month number with leading zeroes
MMM = abbreviated month name
MMMM = full month name
So:
$Month = Get-Date -f "M. MMMM"
> $Month
12. December
As the format string can contain any letter you can build the source path in one step:
(escaped with a backslash if interfering with a format letter)
$Source = "F:\BB\{0:yyyy\\M. MMMM}\Scan.pdf" -f (Get-Date)
> $Source
F:\BB\2018\12. Dezember\Scan.pdf
But I'm missing the days here?
If you use
$month = Get-Date -Format "MM"
this will get you the month as a number. If I understand what you are trying to achive this should match you source path.
$Date = get-date
$Path = "F:\BB\" + "$($Date.year)" + "\" + "$($Date.month)" + "\"
Copy-Item -Path $Path -Destination F:\BB
I want to convert datetime to Epoch format in csv file using PowerShell. In the csv file I have only time data, and I want to use current date and time specified in csv to convert it to Epoch format .
in.csv
"192.168.1.2","01-any1TEST ","Ping","Down at least 3 min","17:25:14",":Windows 2012 Server"
"192.168.1.2","02-any2TEST ","Ping","Down at least 4 min","17:25:40",":Unix Server"
"192.168.1.2","03-any3TEST ","Ping","Down at least 3 min","17:26:21",":windows host "
My findings
This should be doable using a combination of the below two. The main issue I am facing is that I am unable to combine the current date with the time in csv file.
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = [datetime]::Parse($_.Date).ToString('MM/dd/yyyy HH:mm')
}
Get-Date -Date "12/31/2015 23:59:59" -UFormat %s
When using Get-Date, you have the option to override values manually or using another datetime
For example:
Import-Csv ".\out.csv" |
ForEach-Object {
$tempDate = [datetime]::Parse($_.Date).ToString('MM/dd/yyyy HH:mm')
Get-Date -Hour $tempDate.Hour -Minute $tempDate.Minute -UFormat %s
}
I'd do it like this:
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = Get-Date -Date $_.Date -UFormat %s
}
If you want to be a bit more explicit about what it's doing, you can convert the time to a timespan, which can be added to a date. Then you can pipe it to Get-Date to format it:
Import-Csv ".\out.csv" |
ForEach-Object {
$_.Date = [DateTime]::Today + [Timespan]::Parse($_.Date) | Get-Date -UFormat %s
}
[DateTime]::Today is today's date at midnight (time 00:00:00).
Ok, try the code below. It will write a warning message to the console when it finds a date that it can't parse. It won't fix the problem, but it will tell you where the problem is.
Import-Csv ".\out.csv" |
ForEach-Object {
$t = [timespan]::Zero
if ([Timespan]::TryParse($_.Date,[ref]$t)) {
$_.Date = [DateTime]::Today + $t | Get-Date -UFormat %s
}
else {
Write-Warning "Unable to parse timespan '$($_.Date)' for record $($_)"
}
}
This is working perfectly for me in 2.0 and not on 4.0 version . If possible please let me know why is is not working on powershell 4.0 .
$EventTime = $($s.EventTime)
$value = get-date -format d
$Imported = Import-Csv 'C:\PathToFIle\out.csv'
$Output = foreach ($i in $Imported) {
foreach ($c in $EventTime) {
$time=$i.eventtime
$fulltime =$value+' '+$time
$i.EventTime = Get-Date -Date $fulltime -UFormat %s
}
$i
}
$Output
$Output | Export-Csv 'C:\PathToFIle\TestComputers.csv' -NoTypeInformation
I am trying to parse a file for only the most recent entries. Each entry in the log file starts with date in the format "m/d/yyyy h:mm:ss PM".
I write a script that accomplishes what I wanted but unfortunately this script runs on Powershell 5 but not Powershell 2
$regex = '\b([1-2][0-9]|[0-9])\/([1-3][0-9]|[0-9])\/(20[0-9][0-9]) ((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9]) ([AaPp][Mm]))'
$lines = gci C:\temp\Gateway.log | select-string -pattern $regex |select linenumber, matches
$log=#()
foreach($line in $lines)
{
[datetime]$logdate = ($line.matches).value
$ln = $line.linenumber
if ($logdate -gt '2017-06-29 12:00:00')
{
$log += $ln
}
}
$log
When i try to run this script on the server with powershell 2, i get the following error.
Cannot convert null to type "System.DateTime".
I am having trouble converting the value found by the select-string into datetime. tried convert to string with tostring and also with out-string
$dt = $line.matches
$val = $dt|select Value
$val1 = $val|Out-String
$val2 = $val.tostring()
[datetime]$val1
[datetime]$val2
How can I get the value as datetime so I can do datemath on in it powershell 2?
To my experience the am/pm conversion doesn't work (also not with [CultureInfo]::InvariantCulture) if your current locale settings don't apply with it.
You need to specify a cultureinfo object which supports am/pm
$enUS = 'en-US' -as [Globalization.CultureInfo]
$val1 = (get-date).ToString("M/d/yyyy H:mm:ss tt", $enUS)
$val1
[DateTime]::ParseExact($val1, "M/d/yyyy H:mm:ss tt", $enUS)
I need to get the end date of the month we are in.
I tried with this code:
$CURRENTDATE=GET-DATE -Format "dd/MM/yyyy"
$FIRSTDAYOFMONTH=GET-DATE $CURRENTDATE -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
echo $LASTDAYOFMONTH
This gives me the date of the end of the month. But i need it in another format (dd/MM/yyyy).
But can't seem to get my head around it. A push in the right direction would be great.
Adapting Mike F. Robbins code
$currentDate = Get-Date
$lastDay = [DateTime]::DaysInMonth($currentDate.Year, $currentDate.Month)
$lastDayOfMonth = Get-Date ([DateTime]"$($currentDate.Month), $LastDay, $($currentDate.Year)") -Format 'dd/MM/yyyy'
Write-Host $lastDayOfMonth
Or if you want to use your existing code:
$CurrentDate = Get-Date -Format "dd/MM/yyyy"
$FirstDayOfMonth = Get-Date $CurrentDate -Day 1
$LastDayOfMonth = Get-Date $FirstDayOfMonth.AddMonths(1).AddSeconds(-1) -Format "dd/MM/yyyy"
Write-Output $LastDayOfMonth