locked
PowerShell : write a log file RRS feed

  • Question

  • I try to make the parallel whith the "BATCH world", and the "PowerShell World" :

    DIR C:\ > d:\toto.txt
    How to write this commande in PowerShell ?

    I try to use "<powershell_commande> | Write-Log D:\toto.txt", with no success....
    As you can see, I'm very interesting to know how to write the result of a command inside a "log file" , using PowerShell.

    • Moved by Emile Supiot Monday, September 28, 2015 10:31 AM written in English
    Tuesday, September 22, 2015 1:36 PM

Answers

  • dir>C:\Log.txt

    in powershell:

    GCI | out-file -filepath C:\Log.txt

    dir>>C:\Log.txt

    in powershell:

    GCI | out-file -filepath C:\Log.txt -append

    • Proposed as answer by Elaine Jing Monday, October 12, 2015 6:25 AM
    • Marked as answer by Elaine Jing Tuesday, October 13, 2015 1:14 AM
    Tuesday, September 22, 2015 2:36 PM

All replies

  • Hi,

    You can just use this :

    ls > log.txt


    Cordialement,

    Mickaël LOPES

    Blog : http://lopes.im 

      


    Tuesday, September 22, 2015 1:39 PM
  • OK, Inside the PowerShell command (GUI), it works.
    Inside a script, NOT.
    Why ?
    --
    I try, without success  :
    "powershell -noprofile -command "write $compteur.Lines" > %replog%\compteur_ligne.txt"
    and
    "powershell -noprofile -command "write $compteur.Lines > %replog%\compteur_ligne.txt""
    ---

    --
    SCRIPT :
    --
    ::
    REM TEST POWERSHELL + BATCH
    ::
    rem powershell -noprofile -command "(gci c:\temp| where {$_.PsIscontainer}).count"
    rem Measure-Object -inputObject -line
    rem get-content C:\test.txt | measure-object -character -line -word
    ::
    :: SAMPLE FILE in %RAPOLD% :
    :: DIR_LDSCAN_SERVER1_20150922_11h51.log
    ::
    set RAPOLD=D:\DATA\DIR_LDSCAN\RAPPORTS_OLDS
    set replog=D:\DATA\DIR_LDSCAN\LOGS
    set repscript=D:\DATA\DIR_LDSCAN\SCRIPTS
    ::
    DIR /B %RAPOLD% > %replog%\DIR_RAPOLD.txt
    ::
    ::
    FOR /F "tokens=1,2 delims=." %%a IN (%replog%\DIR_RAPOLD.txt) Do (
    ECHO %%a > %replog%\DIR_RAPOLD_2.txt
    FOR /F "tokens=1,2,3,4,5 delims=_" %%c IN (%replog%\DIR_RAPOLD_2.txt) Do (
    set srv=%%e
    set datej=%%f
    set heurej=%%g
    powershell -noprofile -command "$compteur = get-content %RAPOLD%\%%a.log | measure-object -line"
    powershell -noprofile -command "write $compteur.Lines"
    powershell -noprofile -command "write $compteur.Lines" > %replog%\compteur_ligne.txt
    notepad %replog%\compteur_ligne.txt
    FOR /F "tokens=1" %%h IN (%replog%\compteur_ligne.txt) DO (
    ECHO %%e;%%f;%%g;%%h; >> %replog%\DIR_RAPOLD_3.txt
    ECHO COMPTEUR:%compteur%
    )
    )
    )
    pause

    --

    Tuesday, September 22, 2015 2:31 PM
  • dir>C:\Log.txt

    in powershell:

    GCI | out-file -filepath C:\Log.txt

    dir>>C:\Log.txt

    in powershell:

    GCI | out-file -filepath C:\Log.txt -append

    • Proposed as answer by Elaine Jing Monday, October 12, 2015 6:25 AM
    • Marked as answer by Elaine Jing Tuesday, October 13, 2015 1:14 AM
    Tuesday, September 22, 2015 2:36 PM