VBA and Powershell error...

Candyman8019

Well-known Member
Joined
Dec 2, 2020
Messages
1,082
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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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