none
Powershell script and output RRS feed

  • Question

  • Good day

    I'm new on the PowerShell script (Linux User), I just inherited few windows machine and some have Windows Shares as I was googling around I found a script that will display users and the last time a folder was accessed. Run the script but with no output

    here is the script

    Get-ChildItem -Recurse "C:\Users\%ProfileName%\Desktop\Users.ps1"  | ?{ $_.PSIsContainer } | Select-Object FullName,lastwritetime | Out-File 'C:\Users\%ProfileName%\Desktop\Test.log'

    Thursday, August 25, 2016 2:49 PM

Answers

  • Hi,

    That's because %profilename% doesn't do anything.

    Here's how to use environment variables in PowerShell:

    http://ss64.com/ps/syntax-env.html

    You also don't need to test on PSIsContainer, just use the -Directory switch of Get-ChildItem.

    Lastly, do not use single quotes if you want to use a variable. Single quotes do not allow variables to expand.


    Thursday, August 25, 2016 2:54 PM
    Moderator
  • Hi New guy, Mike,

    first of all: Welcome to Technet, hope you have a good time here and find answers to whatever it may be you seek :)

    A few pieces of advice on your objective:

    • Use Export-Csv, if you want to export objects to file. It gives you a neat table you can use in Excel
    • For environment variables: See Mike's link. The notation you use is old batch script notation.
    • Stick with "? { $_.PSIsContainer }", if you may need to run this on machines that have PowerShell v2. Those don't have the more efficient -Directory switch on Get-Childitem.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, August 25, 2016 3:18 PM

All replies

  • Hi,

    That's because %profilename% doesn't do anything.

    Here's how to use environment variables in PowerShell:

    http://ss64.com/ps/syntax-env.html

    You also don't need to test on PSIsContainer, just use the -Directory switch of Get-ChildItem.

    Lastly, do not use single quotes if you want to use a variable. Single quotes do not allow variables to expand.


    Thursday, August 25, 2016 2:54 PM
    Moderator
  • Hi New guy, Mike,

    first of all: Welcome to Technet, hope you have a good time here and find answers to whatever it may be you seek :)

    A few pieces of advice on your objective:

    • Use Export-Csv, if you want to export objects to file. It gives you a neat table you can use in Excel
    • For environment variables: See Mike's link. The notation you use is old batch script notation.
    • Stick with "? { $_.PSIsContainer }", if you may need to run this on machines that have PowerShell v2. Those don't have the more efficient -Directory switch on Get-Childitem.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, August 25, 2016 3:18 PM