82780

Check for File in multiple directories with an IF statement Powershell

Question:

I am facing some problems in powershell. I want to be able to let a powershell command search for multiple directories. With the name being a variable like "$VM_DISK=VM_DISK.vhdx" and let powershell search in that manor so that if that file exists in a folder such as C:\VM_DISK\ it exit the script. I have already tried the "Get-Childitem" but it doesn't seem to work when I put my variable in it. Here is an example:

$VM_DISK= "Example.vhdx" $search=Get-ChildItem -Path C:\VM_DISK\* -Filter $VM_DISK -Recurse if ($search -eq $VM_DISK) {write-host "Goodbye!" exit} else {write-host "Continue"}

I just cant seem to figure out why this isn't working, hope some can figure it out.

Answer1:

You need to alter your if statement.

if ($search.Name -contains $VM_Disk)

This way you are comparing an Array of names (which is what you want, names of objects, not objects) to a name of particular object (to a string, basically).<br /> This makes little sense in your case, tbh. Since $search would always include $VM_Disk or would be null if nothing was found.

So the proper way to test would be if ($search) (just like Mathias advised). Which would test if anything was returned. Which, basically equals what you are trying to do.

Recommend

  • Get-Content : Cannot find path
  • Powershell Copy files and folders
  • how to convert filenames with special characters to valid filenames?
  • Powershell and last modified date
  • Combine content of several files in folder
  • Powershell script to find all svn working copies
  • self-reflexive n:m relation using composite keys in nhibernate
  • Turn list output into an array of objects or something similar using Powershell
  • Aggregating By Date in Mongodb
  • Advantage of 'one dimensional' hash over array in Perl
  • What does a hyphen at end of a term mean
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • smarty nested if condition is not working properly?
  • How to resolve docker host names (/etc/hosts) in containers
  • Ubuntu and bcrypt
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • Java: can you cast Class into a specific interface?
  • Paperclip, set path outside of rails root folder
  • DomPDF {PAGE_NUM} not on first page
  • Email format validation in mvc3 view
  • Javascript simulate pressing enter in input box
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • AES padding and writing the ciphertext to a disk file
  • How to add date and time under each post in guestbook in google app engine
  • Obtain ObjectIdHex value from mgo query
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Importing jscolor library in angular 2
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • A cron job substitute?
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • coudnt use logback because of log4j
  • need help with bizarre java.net.HttpURLConnection behavior
  • Append folder name and increment by 1 using batch script
  • How to load view controller without button in storyboard?