clear-content can't clear the line because file is in used !!!

Hello

the following lines results to a text file on my desktop which it contains following two lines (my ipv4 address & ipv6 address):

10.1.1.1
fe80::d4c4:49d4:5a94:b92a

now i need a code to clear the 2nd line (ipv6 address), but i got the following error:

" the process cannot access the file....."

but the file is not in used & every window & App is closed. i tried it many times even logoff & login again .

i studies both get-content & set-content help files & i searched this in Google but no related result:

"get-content piped to clear-content error can't access the file..."

any idea?

new-item -name 'MyipAddress.txt' -Path "$home\desktop" -ItemType file -Force

$file = "$home\desktop\MyipAddress.txt"

$nic = gwmi -class win32_networkadapterconfiguration | where index -eq 14

$nic.IPAddress | Add-Content -Path $file

Get-Content $file -Tail 1 | Clear-Content -Force


  • Edited by john.s2011 Saturday, January 31, 2015 3:04 PM
January 31st, 2015 6:02pm

Sorry but Clear-Content takes a file as an input. When it is called it receives only one line at a time .   Clear-COntent takes a file as input and only clears a whole file.

help clear-content -full

Always read the manual before giving up or postin a question.

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 8:56am

You are passing a line of text to a CmdLet that require a file object as input.  That is what the help says.  REread it and thik about what it is trying to tell you.  What "Provider" are you specifying?  You are passing an IPAddress to a CmdLet that consumes fileobjects.

February 1st, 2015 9:25am

You are passing a line of text to a CmdLet that require a file object as input.  That is what the help says.  REread it and thik about what it is trying to tell you.  What "Provider" are you specifying?  You are passing an IPAddress to a CmdLet that consumes fileobjects.

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 5:06pm

You are passing a line of text to a CmdLet that require a file object as input.  That is what the help says.  REread it and thik about what it is trying to tell you.  What "Provider" are you specifying?  You are passing an IPAddress to a CmdLet that consumes fileobjects.

February 1st, 2015 5:45pm

Hello

the following lines results to a text file on my desktop which it contains following two lines (my ipv4 address & ipv6 address):

10.1.1.1
fe80::d4c4:49d4:5a94:b92a

now i need a code to clear the 2nd line (ipv6 address), but i got the following error:

...


Ignore this rubbishell coder mvp; *it* never helps anyone.

*It* just keep posting useless comments that deserves to be thrown into a trash.

Usually, *its* code needs correction; that's why *it*'s always changing *its* code!

=

The solution to your problem is simple.

You have a file and you want to delete the 2nd line (i.e. index 1), so use this very simple code:

          (gc $file)[,0+2..99] | out-file $file    # $file must contain no more than 100 lines for this example.

Simple.

P.S.: ignore the trash and you'll improve your skill faster and correctly!

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2015 1:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics