PoshCode Archive  Hex Artifact Content

Artifact 0c9d643d1b5514af8012a6db1fce11ef85b199b9769ae622dcd886755a688b71:

  • File Time-Stamp.ps1 — part of check-in [32c218cf20] at 2018-06-10 13:15:54 on branch trunk — This is a very simple function that returns a DateTime time stamp. I use it in scripts for noting times when actions occur like this Write-Host “$(Time-Stamp): Attempting to create directory.”; It’s a lot shorter than writing out the whole thing. You can also alias it to simplify the process: New-Alias -Name ts -Value Time-Stamp;. This way I use Write-Host “$(ts): Attempting to create directory.”; It outputs: 2011.07.17 05:01:10: Attempting to create directory. You can alter the value in the ToString to change the way the time stamp is formatted. (user: Will Steele size: 977)

0000: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 61 73 63 69  # encoding: asci
0010: 69 0d 0a 23 20 61 70 69 3a 20 70 6f 77 65 72 73  i..# api: powers
0020: 68 65 6c 6c 0d 0a 23 20 74 69 74 6c 65 3a 20 54  hell..# title: T
0030: 69 6d 65 2d 53 74 61 6d 70 0d 0a 23 20 64 65 73  ime-Stamp..# des
0040: 63 72 69 70 74 69 6f 6e 3a 20 54 68 69 73 20 69  cription: This i
0050: 73 20 61 20 76 65 72 79 20 73 69 6d 70 6c 65 20  s a very simple 
0060: 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65  function that re
0070: 74 75 72 6e 73 20 61 20 44 61 74 65 54 69 6d 65  turns a DateTime
0080: 20 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 49 20   time stamp.  I 
0090: 75 73 65 20 69 74 20 69 6e 20 73 63 72 69 70 74  use it in script
00a0: 73 20 66 6f 72 20 6e 6f 74 69 6e 67 20 74 69 6d  s for noting tim
00b0: 65 73 20 77 68 65 6e 20 61 63 74 69 6f 6e 73 20  es when actions 
00c0: 6f 63 63 75 72 20 6c 69 6b 65 20 74 68 69 73 20  occur like this 
00d0: 57 72 69 74 65 2d 48 6f 73 74 20 e2 80 9c 24 28  Write-Host ...$(
00e0: 54 69 6d 65 2d 53 74 61 6d 70 29 3a 20 41 74 74  Time-Stamp): Att
00f0: 65 6d 70 74 69 6e 67 20 74 6f 20 63 72 65 61 74  empting to creat
0100: 65 20 64 69 72 65 63 74 6f 72 79 2e e2 80 9d 3b  e directory....;
0110: 20 20 49 74 e2 80 99 73 20 61 20 6c 6f 74 20 73    It...s a lot s
0120: 68 6f 72 74 65 72 20 74 68 61 6e 20 77 72 69 74  horter than writ
0130: 69 6e 67 20 6f 75 74 20 74 68 65 20 77 68 6f 6c  ing out the whol
0140: 65 20 74 68 69 6e 67 2e 20 20 59 6f 75 20 63 61  e thing.  You ca
0150: 6e 20 61 6c 73 6f 20 61 6c 69 61 73 20 69 74 20  n also alias it 
0160: 74 6f 20 73 69 6d 70 6c 69 66 79 20 74 68 65 20  to simplify the 
0170: 70 72 6f 63 65 73 73 3a 20 4e 65 77 2d 41 6c 69  process: New-Ali
0180: 61 73 20 2d 4e 61 6d 65 20 74 73 20 2d 56 61 6c  as -Name ts -Val
0190: 75 65 20 54 69 6d 65 2d 53 74 61 6d 70 3b 2e 20  ue Time-Stamp;. 
01a0: 20 54 68 69 73 20 77 61 79 20 49 20 75 73 65 20   This way I use 
01b0: 57 72 69 74 65 2d 48 6f 73 74 20 e2 80 9c 24 28  Write-Host ...$(
01c0: 74 73 29 3a 20 41 74 74 65 6d 70 74 69 6e 67 20  ts): Attempting 
01d0: 74 6f 20 63 72 65 61 74 65 20 64 69 72 65 63 74  to create direct
01e0: 6f 72 79 2e e2 80 9d 3b 20 20 49 74 20 6f 75 74  ory....;  It out
01f0: 70 75 74 73 3a 20 32 30 31 31 2e 30 37 2e 31 37  puts: 2011.07.17
0200: 20 30 35 3a 30 31 3a 31 30 3a 20 41 74 74 65 6d   05:01:10: Attem
0210: 70 74 69 6e 67 20 74 6f 20 63 72 65 61 74 65 20  pting to create 
0220: 64 69 72 65 63 74 6f 72 79 2e 20 20 59 6f 75 20  directory.  You 
0230: 63 61 6e 20 61 6c 74 65 72 20 74 68 65 20 76 61  can alter the va
0240: 6c 75 65 20 69 6e 20 74 68 65 20 54 6f 53 74 72  lue in the ToStr
0250: 69 6e 67 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ing to change th
0260: 65 20 77 61 79 20 74 68 65 20 74 69 6d 65 20 73  e way the time s
0270: 74 61 6d 70 20 69 73 20 66 6f 72 6d 61 74 74 65  tamp is formatte
0280: 64 2e 0d 0a 23 20 76 65 72 73 69 6f 6e 3a 20 30  d...# version: 0
0290: 2e 31 0d 0a 23 20 74 79 70 65 3a 20 66 75 6e 63  .1..# type: func
02a0: 74 69 6f 6e 0d 0a 23 20 61 75 74 68 6f 72 3a 20  tion..# author: 
02b0: 57 69 6c 6c 20 53 74 65 65 6c 65 0d 0a 23 20 6c  Will Steele..# l
02c0: 69 63 65 6e 73 65 3a 20 43 43 30 0d 0a 23 20 66  icense: CC0..# f
02d0: 75 6e 63 74 69 6f 6e 3a 20 54 69 6d 65 2d 53 74  unction: Time-St
02e0: 61 6d 70 0d 0a 23 20 78 2d 70 6f 73 68 63 6f 64  amp..# x-poshcod
02f0: 65 2d 69 64 3a 20 32 37 39 35 0d 0a 23 20 78 2d  e-id: 2795..# x-
0300: 61 72 63 68 69 76 65 64 3a 20 32 30 31 36 2d 31  archived: 2016-1
0310: 30 2d 33 30 54 30 32 3a 30 39 3a 30 32 0d 0a 23  0-30T02:09:02..#
0320: 20 78 2d 70 75 62 6c 69 73 68 65 64 3a 20 32 30   x-published: 20
0330: 31 31 2d 30 37 2d 31 37 54 31 35 3a 30 33 3a 30  11-07-17T15:03:0
0340: 30 0d 0a 23 0d 0a 23 0d 0a 66 75 6e 63 74 69 6f  0..#..#..functio
0350: 6e 20 54 69 6d 65 2d 53 74 61 6d 70 0d 0a 7b 0d  n Time-Stamp..{.
0360: 0a 20 20 20 20 72 65 74 75 72 6e 20 5b 53 79 73  .    return [Sys
0370: 74 65 6d 2e 44 61 74 65 54 69 6d 65 5d 3a 3a 4e  tem.DateTime]::N
0380: 6f 77 2e 54 6f 53 74 72 69 6e 67 28 22 79 79 79  ow.ToString("yyy
0390: 79 2e 4d 4d 2e 64 64 20 68 68 3a 6d 6d 3a 73 73  y.MM.dd hh:mm:ss
03a0: 22 29 3b 0d 0a 7d 0d 0a 0d 0a 4e 65 77 2d 41 6c  ");..}....New-Al
03b0: 69 61 73 20 2d 4e 61 6d 65 20 74 73 20 2d 56 61  ias -Name ts -Va
03c0: 6c 75 65 20 54 69 6d 65 2d 53 74 61 6d 70 3b 0d  lue Time-Stamp;.
03d0: 0a                                               .