r/PowerShell • • Oct 22 '26

Question Environment stopped handling large files

Edit: To everyone who responded. You've given me some good ideas, and now I have to experiment to track down where the issue lies. Thanks to all.

I have a workflow which takes a comma delimited text file (*.txt) and updates an Azure database with the values. The workflow is called by a PowerShell script on a remote Windows Server, which looks to a folder and finds all the txt files in there, and runs the workflow against each one. This process worked fine for about two years. Then last spring, I started getting reports from our users that not all the values that should be updated in the database where. No one changed the workflow, and no one changed the PowerShell script.

After a lot of testing, we found that if the txt file was larger than 2K, only the first 2K worth of lines in the file were processed, and everything after was ignored.

On the suggestion of a consultant, he had me run the PowerShell script which calls the workflow from my own laptop. Yes, I had to duplicate the folder structure to make that happen. Much to my surprise, running the script and workflow from my laptop worked just fine.

So I started looking into what in the environment of the Windows Server would limit the ability to pass all the necessary data to the workflow. Of course our server teams swears they did nothing to the server, no updates that would cause this restriction. But I don't trust them to know what the consequences of their actions would be.

After half a year of cutting the text file down into 2K chunks every week (I automated the process, but still), I am again trying to find what the cause of this could be. Script works fine on my laptop, but choaks at 2K file size when run on a Windows Server. The server is more than powerful enough in terms of memory, processor and disk size.

If you have any clue, please pass it on.

2 Upvotes

24 comments sorted by

4

u/BetrayedMilk Oct 22 '26

What is 2K? Like 2000 lines? 2KB? Why not add a counter (either incrementing on line number or whatever 2K is) to the script and add a breakpoint right before this threshold and debug it from the server? Or just add some logging temporarily.

3

u/creenis_blinkum Oct 24 '26

How could you not just run it from the server and… troubleshoot lol

2

u/CarrotBusiness2380 Oct 22 '26

I would guess it is a limit on how you are feeding the rows to the DB rather than something in the environment.

I know you said you tested locally before but I would test again. I have missed obvious things many times because I was in a rush or had a desire to not find a problem.

2

u/PinchesTheCrab Oct 23 '26

Need some code examples, because there's no particular reason why it should fail like that.

1

u/dodexahedron Oct 22 '26 edited Oct 22 '26

Can't you just use bcp?

If it is CSV-formatted, that's the proper way to be bulk loading data from files in the first place.

Also, if you mean 2K as in exactly consistently the same number of bytes be it 2000 or 2048, it sounds suspiciously like someone used a fixed-length char array somewhere like for a file io buffer of 4k and then, when output as utf8 text, the characters are half the size. I've seen that one before. And then that followed by using somwthing like File.WriteAllBytes from that buffer would just overwrite it.

Or the same concepts, but on the input side.

Or both.

Without seeing the input and output portion of the code, all we can do is speculate.

1

u/ka-splam Oct 23 '26

The server is more than powerful enough in terms of memory, processor and disk size.

😂

This process worked fine for about two years. Then last spring

Is it possible there were no files bigger than 2K before last spring?

1

u/Pvt-Snafu Oct 24 '26

A 2K cutoff smells like a buffer limit in how the file gets passed, not the file itself. Compare $PSVersionTable and the execution policy or remoting limits on the server versus your laptop.

1

u/xXFl1ppyXx Oct 24 '26

Are you using a stream or a Powershell cmdlet? Or even import CSV?

If not stream you definitely should try to do that. a csv is easy enough to split after the fact with one regex 

1

u/RichDinero Oct 24 '26

I've done this for CSV files that have millions of rows. These CSV files will not even open because they are so large. By default, Excel will try to open. Once I forced it to open no new window it ran in the background for a while but ultimately was very successful with the file that you can't even open in notepad or Excel.

-5

u/DonJuanDoja Oct 22 '26

Google "powershell only processing txt file up to 2k size limit" it'll tell you stuff that might help.

1

u/charleswj Oct 22 '26

You're kidding right?

-4

u/DonJuanDoja Oct 22 '26

Nope. It can write paragraphs faster than I can so it's the easiest fastest way to get you what you need. Good luck.

1

u/charleswj Oct 22 '26

So you spent the time to comment that a tool everyone knows exists "may" have an answer (it doesn't)? It's like showing up at the hospital and being told "maybe see a doctor?", and then walking away

-4

u/DonJuanDoja Oct 22 '26

Many people lack even basic Google skills, did you google the exact phrase I gave and read it or not? Guessing not. Good luck

1

u/charleswj Oct 22 '26

There are exactly zero results for that phrase

1

u/DonJuanDoja Oct 22 '26

Slanderous lies. Here's the first option it gives me. I don't have special google. You must've done something to yours to disable AI responses. Gemeni is a True G. You just have to be very specific.

  1. WinRM/PowerShell Remoting Quotas (WSMan)

If you are running this script on the server via Enter-PSSession, Invoke-Command, or an automation tool like Ansible/Jenkins, you are subject to WinRM (WS-Management) constraints.

  • The Problem: The MaxEnvelopeSizekb policy limits the maximum amount of data (in kilobytes) that a remote PowerShell session can transmit back to you. The default limit on many server configurations is strictly enforced.
  • The Fix: Run this command on the server to check your limit: powershellGet-Item WSMan:\localhost\MaxEnvelopeSizekb Use code with caution. To increase it (e.g., to 8MB / 8192 KB), open PowerShell as an Administrator on the server and execute: powershellSet-Item WSMan:\localhost\MaxEnvelopeSizekb 8192

2

u/charleswj Oct 22 '26
  1. The exact phrase you offered has quotes and that exact phrase has zero results, AI slop or otherwise.
  2. OP doesn't need any Google "skills" say the same thing in a prompt as they did here
  3. None of those examples are OP's problem

0

u/DonJuanDoja Oct 22 '26
  1. You didn't even try to help OP. Yet you tell me I'm wrong.
  2. That means you're just here to harass anyone you can. Why? Who knows.
  3. You couldn't bother me if you tried your hardest, I can see you're trying, but it's not really working. You don't bother me at all. I just think you're wrong and well kinda weird.
  4. Without the actual scripts, you don't know, neither do I. All we can do is guess.
  5. I don't like AI either, but it helps me solve problems ESPECIALLY powershell problems, every single day.
  6. I get paid for this, and don't need your help or advice in ANY way. If you notice, I don't ask very often, I'm mostly helping others.

2

u/krzydoug Oct 22 '26

It actually sounds like this might be the problem. "Runs a script on a remote windows host" so that implies OP is using psremoting. Considering the default is 500KB that could contribute.

2

u/BlackV Oct 23 '26

"powershell only processing txt file up to 2k size limit"

that phrase into gemini gives me only responses about out-file and its ilk

PowerShell itself does not have a built-in 2KB size limit for processing text files; it can comfortably handle gigabyte-sized files. If your script is getting cut off or truncating data at exactly 2,048 bytes or characters, it is almost always caused by one of the following hidden formatting or buffer limits:

  1. Truncation from Out-File or Redirection (>)

<snip> 2. Formatted Table Truncation (Format-Table) <snip> 3. XML Node .InnerXml / .InnerText 2,048-Character Display Cap <snip>

but I do agree that MaxEnvelopeSizekb is possibly the problem OP is having

although I'd have thought the workstation and the server would behave in the same manner if that was the issue

-2

u/DonJuanDoja Oct 22 '26

Option 2. A Headless Console Width (No Interactive Desktop)

When a script runs on a server via a Task Scheduler, SQL Server Agent, or a service account, it runs in a "headless" state with no physical monitor or interactive window.

  • The Problem: In a headless state, PowerShell generates a default virtual console width (often defaults to 80 characters, but some host environments default to a strict buffer size limit). If your script uses pipeline outputs or formatting cmdlets like Out-File, PowerShell will cut the text string at the console buffer boundary.
  • The Fix: Force PowerShell to bypass the console width entirely by specifying an explicit width or using a direct string exporter: powershell# Instead of Out-File: $Data | Out-File -FilePath "C:\output.txt" -Width 10000 # Even better, use Set-Content (which ignores screen layout entirely): $Data | Set-Content -Path "C:\output.txt"

-2

u/DonJuanDoja Oct 22 '26

Option 3.

3. Execution via Restricted API or Group Policy (AppLocker / Constrained Language Mode)

Servers are frequently hardened using Group Policies that force PowerShell into Constrained Language Mode to prevent exploits.

  • The Problem: In Constrained Language Mode, complex .NET data objects are strictly prohibited, and variables passed into pipelines or commands can be aggressively string-serialized or truncated down to tiny safety limits.
  • The Fix: Check your server's language mode by executing: powershell$ExecutionContext.SessionState.LanguageMode Use code with caution. If it returns ConstrainedLanguage, your text processing limitations are built-in security policies. You will need to ask your systems administrator for an exception or refactor the script to use basic, permitted elements.

-2

u/DonJuanDoja Oct 22 '26

Also read you can try the -raw flag, I don't have your script so I don't know what methods it's using etc.