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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:05:53+00:00 2026-06-01T18:05:53+00:00

I have this powershell script running. The first time it runs it runs flawlessly,

  • 0

I have this powershell script running. The first time it runs it runs flawlessly, the second time it runs i get the error that the .csv cannont be access “because it is being used by another process. Any idea which part of the script is “holding onto” the file and how i can make it let it go at the end?

clear
set-executionpolicy remotesigned
# change this to the directory that the script is sitting in
cd d:\directory

#############################################
# Saves usernames/accountNumbers into array # 
#    and creates sql file for writing to    #
#############################################


# This is the location of the text file containing accounts
$accountNumbers = (Get-Content input.txt) | Sort-Object
$accountID=0
$numAccounts = $accountNumbers.Count
$outString =$null
# the name of the sql file containing the query
$file = New-Item -ItemType file -name sql.sql -Force


###################################
# Load SqlServerProviderSnapin100 #
###################################

if (!(Get-PSSnapin | ?{$_.name -eq 'SqlServerProviderSnapin110'})) 
{ 
    if(Get-PSSnapin -registered | ?{$_.name -eq 'SqlServerProviderSnapin110'}) 
    { 
        add-pssnapin SqlServerProviderSnapin100
        Write-host SQL Server Provider Snapin Loaded
    } 
    else 
    { 
    } 
} 
else 
{ 
Write-host SQL Server Provider Snapin was already loaded
}  


#################################
# Load SqlServerCmdletSnapin100 #
#################################

if (!(Get-PSSnapin | ?{$_.name -eq 'SqlServerCmdletSnapin100'})) 
{ 
    if(Get-PSSnapin -registered | ?{$_.name -eq 'SqlServerCmdletSnapin100'}) 
    { 
        add-pssnapin SqlServerCmdletSnapin100
        Write-host SQL Server Cmdlet Snapin Loaded
    } 
    else 
    { 

    } 
} 
else 
{ 
    Write-host SQL Server CMDlet Snapin was already loaded
} 




####################
# Create SQL query #
####################

# This part of the query is COMPLETELY static. What is put in here will not change. It will usually end with either % or '
$outString = "SELECT stuff FROM table LIKE '%"
# Statement ends at '. loop adds in "xxx' or like 'xxx"
IF ($numAccounts -gt 0)
{
    For ($i =1; $i -le ($AccountNumbers.Count - 1); $i++)
    {
        $outString = $outstring + $AccountNumbers[$accountID]
        $outString = $outString + "' OR ca.accountnumber LIKE '"
        $accountID++
    }
    $outString = $outString + $AccountNumbers[$AccountNumbers.Count - 1] 
}
else
{
    $outString = $outString + $AccountNumbers
}
# This is the end of the query. This is also COMPLETELY static. usually starts with either % or '
$outString = $outString + "%'more sql stuff"
add-content $file $outString
Write-host Sql query dynamically written and saved to file



###########################
# Create CSV to email out #
###########################

#Make sure to point it to the correct input file (sql query made above) and correct output csv. 
Invoke-Sqlcmd -ServerInstance instance -Database database -Username username -Password password -InputFile sql.sql | Export-Csv -Path output.csv



####################################
# Email the CSV to selected people #
####################################

$emailFrom = "to"
$emailTo = "from"
$subject = "test"
$body = "test"
$smtpServer = "server"
# Point this to the correct csv created above
$filename = "output.csv"

$att = new-object Net.mail.attachment($filename)
$msg = new-object net.mail.mailmessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.from = $emailFrom
$msg.to.add($emailto)
$msg.subject = $subject
$msg.body = $body
$msg.attachments.add($att)

$smtp.Send($msg)
  • 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-01T18:05:54+00:00Added an answer on June 1, 2026 at 6:05 pm

    Can you try to add at th end :

    $att.Dispose()
    $msg.Dispose()
    $smtp.Dispose()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Powershell script that needs to be run automatically daily. This
I have this bit of powershell script but i can't get the $DirectoryName to
I am struggling to get this simple PowerShell script to work. I have googled,
I have a powershell script that runs a console .exe application. The console application
I'm pretty new to Powershell. I have 2 different scripts I'm running that I
I have powershell script which sends text message through a sms-gateway. I get the
I currently have a PowerShell script that is used for complex automated Exchange mailbox
I have a PowerShell script that configures web site and web application settings in
I'm attempting to create an ASP.NET/C# page that runs a PowerShell script that will
I have a simple PowerShell script to stop a process: $p = get-process $args

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.