0

I am trying to run the PowerShell command Test-Path in VBA. But I cannot get it to work. I want to check if a file exists on a remote computer or on a NAS. The DIR function does not work. Here is the VBA code that I am using.

Dim boolFileExists as Boolean

boolFileExists = "PowerShell -Command ""{Test-Path 'D:\Data\Exercise\Chair Workouts.docx'}"""

Thank you

3
  • 1
    Why using powershell in VBA for that? Please use "Scripting.FilesystemObject" to determine whether a filesystem item exists or not. With powershell you only get some text returned which needs to be parsed. No methods available. learn.microsoft.com/de-de/office/vba/language/reference/… Commented Sep 25, 2020 at 15:02
  • Agree with @pwndsh. I presume VBA is going to complain if you are trying to run an outside executable. That strikes me as a rather intolerable attack vector. Commented Sep 25, 2020 at 15:37
  • Thank you, I was able to run the PowerShell command, but I was not able to get a return value. I took pwndsh's advice to use the Scripting.FilesystemObject. It worked perfectly. Thank you very much. Commented Sep 29, 2020 at 15:35

1 Answer 1

0

The comments are right but if you want to use Powershell you need to use VBA's Shell command to do it: PowerShell Command from VBA

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was able to run the PowerShell command, but I was not able to get a return value. I took pwndsh's advice to use the Scripting.FilesystemObject. It worked perfectly. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.