Creating or modifying a registry value
The following example creates the two registry values
foo
and
bar
under
HKCU\Software\TQ84
:
$regKey = 'hkcu:\Software\TQ84'
if (! (test-path $regKey )) {
$null = new-item $regKey
}
set-itemProperty `
$regKey `
-name foo `
-type dWord `
-value 42
set-itemProperty `
$regKey `
-name bar `
-type string `
-value 'hello World'
Simulating the shell touch command
set-itemProperty
can be used to simulated the shell
touch
command to change a file's last write time:
$fileName = 'test'
set-content $fileName 'foo bar baz'
set-itemProperty $fileName lastWriteTime (get-date '2001-02-03T04:05:06')
get-item $fileName
#
# Directory: C:\Users\r.nyffenegger\github\temp\PowerShell\cmdLets\itemProperty\set
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a--- 03.02.2001 04:05 13 test
Alternatively, the last modification time etc. can also be altered like so:
$f = get-item c:\users\rene\xyz.txt
$f.lastAccessTime = new-object dateTime 2020,08,28,22,23,01
$f.lastWriteTime = new-object dateTime 2001,02,03,04,05,06
$f.creationTime = (get-date).addDays(-5)