Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:57:04+00:00 2026-06-15T16:57:04+00:00

I have part of a deploy PowerShell 2.0 script that copy a potential robots.dev.txt

  • 0

I have part of a deploy PowerShell 2.0 script that copy a potential robots.dev.txt to robots.txt, if it doesn’t exist don’t do anything.

My original code was:

$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt"
    foreach($file in $RobotFilesToOverWrite)
    {
        $origin=$file
        $destination=$file -replace ".$Environment.","."

        #Copy-Item $origin $destination
    }

But, in a difference with C#, even if $RobotFilesToOverWrite is null, code is entering in the foreach.

So I had to surround everything with:

if($RobotFilesToOverWrite)
{
    ...
}

This is the final code:

$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt"
if($RobotFilesToOverWrite)
{
    foreach($file in $RobotFilesToOverWrite)
    {
        $origin=$file
        $destination=$file -replace ".$Environment.","."

        #Copy-Item $origin $destination
    }
}

I was wondering if there is a better way to achieve that?

EDIT: This problem seems to be fixed in PowerShell 3.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:57:05+00:00Added an answer on June 15, 2026 at 4:57 pm
    # one way is using @(), it ensures an array always, i.e. empty instead of null
    $RobotFilesToOverWrite = @(Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt")
    foreach($file in $RobotFilesToOverWrite)
    {
        ...
    }
    
    # another way (if possible) is not to use an intermediate variable
    foreach($file in Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt")
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have part of my script that goes like this: while read line do
I have a website that I want to deploy to a clients DEV and
I have this part of script from my GAE application which uses webapp2, which
I have a part of my app that takes a screenshot of a certain
I have a ps1 script that deploys all of my webparts. I started noticing
I'm using FM to deploy a database. As a part of that deploy, I'm
I have created a SharePoint web part which has custom properties that the user
I have a WCF project in Visual Studio that I need to deploy to
I need to deploy a webservice as part of a DotNetNuke 4.x module that
I have created a powershell module (.psm1) file that includes a few other powershell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.