diff --git a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
index 6721edfa71..2bb8053fda 100644
--- a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
+++ b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
@@ -14,8 +14,7 @@ $node->init;
 $node->start;
 
 my $numrows = 700;
-my $libpq_pipeline = "$ENV{TESTDIR}/libpq_pipeline";
-
+my $libpq_pipeline = TestLib::find_built_program('libpq_pipeline');
 my ($out, $err) = run_command([ $libpq_pipeline, 'tests' ]);
 die "oops: $err" unless $err eq '';
 my @tests = split(/\s+/, $out);
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index 06aae1760e..89e16a45c7 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -75,6 +75,7 @@ our @EXPORT = qw(
   system_log
   run_log
   run_command
+  find_built_program
 
   command_ok
   command_fails
@@ -1000,6 +1001,41 @@ sub command_checks_all
 
 =pod
 
+=item find_built_program(program_name)
+
+Find the ath to an uninstalled built program according to the build type.
+
+Arguments:
+
+=over
+
+=item C<program_name>: name (without extension) of the program to look for
+
+=back
+
+=cut
+
+sub find_built_program
+{
+	my $program = shift;
+	my $path;
+
+	if (defined $ENV{MSBUILDDIR})
+	{
+		# vcregress.pl sets MSBUILDDIR which is the root of all the build dirs
+		my $wanted = sub { $_ eq "$program.exe" && $path = $File::Find::name;};
+		File::Find::find($wanted,$ENV{MSBUILDDIR});
+	}
+	elsif (defined $ENV{TESTDIR} && -e "$ENV{TESTDIR}/$program")
+	{
+		# non-MSVC programs are built in situ in the build tree
+		$path = "$ENV{TESTDIR}/$program";
+	}
+	return $path;
+}
+
+=pod
+
 =back
 
 =cut
diff --git a/src/tools/msvc/vcregress.pl b/src/tools/msvc/vcregress.pl
index 35e8f67f01..7d46b9f0cc 100644
--- a/src/tools/msvc/vcregress.pl
+++ b/src/tools/msvc/vcregress.pl
@@ -60,6 +60,7 @@ copy("$Config/regress/regress.dll",               "src/test/regress");
 copy("$Config/dummy_seclabel/dummy_seclabel.dll", "src/test/regress");
 
 $ENV{PATH} = "$topdir/$Config/libpq;$ENV{PATH}";
+$ENV{MSBUILDDIR} = "$topdir/$Config";
 
 if ($ENV{PERL5LIB})
 {
