VBA and Powershell error...

Candyman8019

Well-known Member
Joined
Dec 2, 2020
Messages
985
Office Version
  1. 365
Platform
  1. Windows
I'm going to start by making it clear I know nil about powershell... I'm trying to create a script that will change the CreationDate of a single file. The odd thing is that if I paste the generated command directly into Powershell it works perfect...but if I run it from VBA, it fails. Code and screenshot of error attached. It seems as though it doesn't like the timestring. I have tried separating it from the date with a 'T'. No change. Any ideas would be appreciated.

VBA Code:
Public Sub CHCreateDate()

Dim PScomm As String
Dim fpath As String
Dim newCreateDateTime As String

fpath = Range("PickedFile").Value
newCreateDateTime = Format(Range("CRDate").Value, "yyyy-mm-dd") & Format(Range("CRTime").Value, "hhmmss")

PScomm = "(Get-Item " & fpath & ").CreationTime = (Get-Date(""" & newCreateDateTime & """))"
'PScomm generates this:  (Get-Item C:\Users\jrieger\Desktop\1014AM\Temp\273.xlsx).CreationTime = (Get-Date("2015-01-12 19:14:25"))

CreateObject("Wscript.Shell").Run "powershell.exe -ExecutionPolicy Bypass -command " & PScomm, windowStyle:=WshHide, waitOnReturn:=True

End Sub

1695403817725.png


Thanks.

Joe
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This works for me, using single quotes around the date and time string.
VBA Code:
    newCreateDateTime = Format(Range("CRDate").Value, "yyyy-mm-dd") & Format(Range("CRTime").Value, " hh:mm:ss")
    
    PScomm = "(Get-Item " & fpath & ").CreationTime = (Get-Date('" & newCreateDateTime & "'))"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,152
Messages
6,123,323
Members
449,094
Latest member
Chestertim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top