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 8773601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:20:25+00:00 2026-06-13T18:20:25+00:00

I need help with creating a function in a script and within that same

  • 0

I need help with creating a function in a script and within that same script calling the function. I have tested this code:

function FUNC1() {
$source="C:\Folder\file.txt"
$destination="\\Server\folder"
$searchFiles = Get-Content "$source"
foreach($filename in $searchFiles){ 
    Test-Path $destination\$filename 
    }
}

function FUNC2() {
$source="C:\Folder\file.txt"
$destination="\\Server\folder"
$searchFiles = Get-Content "$source"
foreach($filename in $searchFiles){ 
    Move-Item C:\folder\$filename $destination -force
    }
}

if (!(FUNC1)) {FUNC2}

However, when testing FUNC1 for false, it does not move anything. When I run the code in the function separately, everything works as it should. Put them together as functions and it is not working. I do not want to create a separate function.ps1 to call, I would rather have my functions called from within the code. Thanks!

  • 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-13T18:20:26+00:00Added an answer on June 13, 2026 at 6:20 pm

    FUNC1 is going to return an array (of booleans) if $searchFiles has two or more filenames in it.

    This will always be true, even if it just contains multiple $false values (because you’re testing the array, not the values it contains). Negating this (the !) will always give $false so the contents of the if will never be executed.

    Your approach seems very strange, where perform all the tests and then the moves whatever the tests showed for that file. I would have expected something like:

    Get-Content "$source" | 
      Where-Object { -not (Test-Path $destination\$_) } |
      Foreach-Object { Move-Item C:\folder\$_ $destination }
    

    Which will iterate over all the lines in $source, ignore cases where a file of that name exists at the destination and move the file to the destination.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just need some help creating a php function out of this code or
I need your help, How would I go about creating a javascript function that
I need help creating PHP code to echo and run a function only 30%
I need help with creating a C# method that returns the index of the
I just need help. Basically I am creating a windows appllication that sends bulk
Need help with a query that I wrote: I have three tables Company id
I need some help on creating a function similar to the putenv() function of
I need help with my code: I am dynamically adding elements, creating new elements
I need help in creating a xml file from my php code. I created
Once again I need some help. I'm creating a multiple file upload function with

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.