Create folder using cmd and passing formatted date from powershell - powershell

I would like to create folder where it's name is date in specific format. Date format is from powershell echo result. How could I combine this two things?
cmd.exe /c md \"%1\%%"date%%\"" /f and powershell get-date -format "{yyyy-MMM-dd HHmm}?
It must be in one line and starts with cmd.
I will appreciate for any help.

This should do what you are looking in powershell:
$format=get-date -format "yyyy-MMM-dd HHmm"
New-Item -ItemType Directory -Path "C:\Folder\$format"
This is the one liner from the cmd prompt:
Powershell -command "New-Item -Name (get-date -format 'yyyy-MMM-dd HHmm') -ItemType Directory -Path 'C:\Folder' -Force"
Hope it helps.

If you want to run it in a single line in the cmd, try this. This will still just powershell to run. But you can run it from a bat file,
powershell $date = get-date -format "{yyyy-MMM-dd HHmm}; md c:\temp\$date
Else you can run it in powershell
$date = get-date -format "{yyyy-MMM-dd HHmm}"
New-Item -ItemType Directory -Path "c:\temp\$date"
Hope this helps.
/Anders

If you're still wanting a right-click context menu entry, (which creates a directory named using the format you've provided), you can do so by just entering this single line into a Command prompt window.
Powershell -NoP -C "New-Item 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir' -Value 'NOW Folder Here' -Force"; "New-Item 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir\command' -Value '\"C:\Windows\system32\WindowsPowerShell\v1.0\Powershell.exe\" -NoP -C \"$dt = get-date -f {yyyy-MMM-dd HHmm}; MD \"\"%V\"\"\$dt\"\"\"' -Force"
This should create a context menu entry named NOW Folder Here, seen when you right click in an empty area of an explorer window, (and probably the desktop). When the entry is selected a powershell command should run creating the directory in that folder.

Related

PowerShell get directory of a program

I recently wrote a script and want to share it with my colleagues. It’s a simple copy and paste program that creates log-files after each run. The problem is that I used this: start transcript -Path C:\Users…
The program works fine but if anyone else runs the script it won’t be able to create log-files, since the directory is a copy of mine.
Now to my question: Is there anyway that the program can find out the directory where each user saved the script so it can create a sub-folder in that directory and then dump the logs in there?
Thank you in advance
The path to the folder containing the currently executing script can be obtained through the $PSScriptRoot automatic variable:
Start-Transcript -OutputDirectory $PSScriptRoot
Here's how I record PowerShell sessions using the Start-Transcript cmdlet. It creates a log file, where the script is run from.
#Log File Paths
$Path = Get-Location
$Path = $Path.ToString()
$Date = Get-Date -Format "yyyy-MM-dd-hh-mm-ss"
$Post = "\" + (Get-Date -Format "yyyy-MM-dd-hh-mm-ss") + "-test.log"
$PostLog = $Path + $Post
$PostLog = $PostLog.ToString()
#Start Transcript
Start-Transcript -Path $PostLog -NoClobber -IncludeInvocationHeader
#Your Script
Get-Date
#End Transcript
Stop-Transcript -ErrorAction Ignore

Install an .MSI file from the same directory as the powershell script without setting a static file path

I have a script that we use that is in Powershell however I need the script to be able to find the files that it needs to install an application dynamically as users can copy the folder to any folder on their computer. Currently I have the below code set, I guess my question is, if the script is in the same folder as the install files. How do I tell powershell to just look in the directory that its being ran from for the install files?
$Install = "C:\Other Folder\File.msi"
$InstallArg = "/quite /norestart"
Start-Process '
-FilePath $Install '
-ArgumentList $InstallArg '
-PassThru | Wait-Process
Any help would be appreciated. Thank you.
Update, I found that I have to be in the directory the script is in. However since we have to run ISE with admin credentials it automatically defaults to C:\Windows\System32 as the directory powershell is looking in regardless if I tell it to open the script. If that is the case how can I tell it to look where the script is located so that it can find the files that it needs?
I have found my answer below is how I got it to work with our current situation. Thank you Thomas for the help!
$ScriptLocation = Get-ChildItem -Path C:\Users -Filter Untitled2.ps1 -Recurse | Select Directory
cd $ScriptLocation.Directory
$Install = ".\Install.msi"
$InstallArg = "/quite /norestart"
Start-Process '
-FilePath $Install '
-ArgumentList $InstallArg '
-PassThru | Wait-Process
Define a relative path:
$Install = ".\File.msi"
If you are not running the script inside the directory, where the script itself and the executable are stored, you will have to determine the absolute path to it. With PowerShell 3+ you can easily determine the directory, where your script is stored, using the $PSScriptRoot variable. You could define your $Install variable like this:
$Install = Join-Path -Path $PSScriptRoot -ChildPath "File.msi"
Thank you so much for your help everyone! I accidentally stumbled upon the perfect solution.
# Set active path to script-location:
$path = $MyInvocation.MyCommand.Path
if (!$path) {
$path = $psISE.CurrentFile.Fullpath
}
if ($path) {
$path = Split-Path $path -Parent
}
Set-Location $path

Powershell command for ReadinessReportCreator.exe

I've got 4,000 shares to go through with the readinessreportcreator, which works when I run it locally, however I put together a command with a foreach loop to read a csv of all the DFS shares, however I need to go deeper into the folder structure, normally I would use the -recurse switch, but that doesn't seem to be working.
The site I got the command from
https://learn.microsoft.com/en-us/deployoffice/use-the-readiness-toolkit-to-assess-application-compatibility-for-office-365-pro
The powershell I have put together is:
$shares = import-csv -path 'C:\temp\dfs.csv'
$shs = ($shares).share
foreach ($sh in $shs)
{
Start-Process -NoNewWindow -FilePath "C:\Program Files (x86)\Microsoft Readiness Toolkit for Office\ReadinessReportCreator.exe" "$sh" Out-File "C:\temp"
write-host "share=" $sh
}
The command the site suggest from a command line is:
ReadinessReportCreator.exe -p c:\officefiles\ -r -output \\server01\finance
I was thinking if I could just use the foreach loop and change c:\officefiles\ with the variable of the share it would run through each folder and subfolder, but it doesn't like the -p, -r or the -output. Porbably because they're not powershell cmdlets, so -p should be -path and -r should be -recurse and -output should be out-file, but onlu out-file is recognised.
Excel file si:
share
\\vmshare\share
\\vm3share\share1
\\vm2share\share2
\\vm2share\share3
Hope this makes sense
Thanks in advance
1)Info from site above:
ReadinessReportCreator.exe -p c:\officefiles\ -r -output \\server01\finance -silent
The following is an example of a command line that you can run to scan a folder, and all its subfolders, and save the results to a network share for the Finance department. This only scans for VBA macros.
2)Can't see structure your csv file, but i have doubt about this rows of code(not sure what in $shs):
$shares = import-csv -path 'C:\temp\dfs.csv'
$shs = ($shares).share
3)When you run command Start-Process you must pass parameters via key -ArgumentList,
-Wait — Wait for the process to be finished before accepting any more inputs.
Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "register --estNumber $Number --postcode `"$PostCode`" --password $Password" -Wait -NoNewWindow
4)For more about Start-Process enter in cli Get-Help Start-Process -Online

Copy-item runas Administrator to program file fail

i would like to copy the license folder and overwrite the existing folder, since it is program file (x86), i have to run the elevated powershell, i am able to copy it when i launch it manually, just wonder is it possible to get all run at one line (all at once) ? really appreicated
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("R:", "\\roa\smdd\Software\Mest", $false)
Start-process Powershell.exe -ArgumentList " Copy-Item "R:\Licenses\" "C:\Program Files `(x86`)\Mest Research S.L\Mest\licenses"" -force -recurse -wait
You don't need to map a drive or invoke powershell.exe. The code is PowerShell, so you don't need to spin up a new copy of PowerShell to run the Copy-Item cmdlet to copy files. You only need one PowerShell command:
Copy-Item "\\roa\smdd\Software\Mest\Licenses\*" "${Env:ProgramFiles(x86)}\Mest Research S.L\Mest\licenses" -Force -Recurse
Note that you will likely need to open PowerShell as administrator (elevated) to be able to copy items into that directory.

How do I make a powershell function that expands filename arguments to include their paths if unqualified?

Starting up notepad++ or many other GUI applications in Windows that will accepted fully qualified filenames of documents on the command line, but which do not accept them if they are not fully qualified, is often done in DOS/Windows batch files like this:
#echo off
start "notepad++" "C:\Program Files (x86)\Notepad++\notepad++.exe" %*
The above, if saved as "npp.cmd" will let you type "npp foo.txt" and it will work.
Note that without the npp.cmd, even typing out the full path to the exe, but not fully qualifying the file to edit doesn't work, like this:
"C:\Program Files (x86)\Notepad++\notepad++.exe" foo.txt
This however, DOES work:
"C:\Program Files (x86)\Notepad++\notepad++.exe" c:\users\warren\foo.txt
A way to easily work around this limitation is to make a batch file (.cmd) as shown at the top of this file. I'm learning PowerShell and trying to find the equivalent magic to the "start .... %*" incantation in the batchfile at the top. I believe it would have to be a 'powershell function'.
Here's what I have so far:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp $args }
The above is equivalent, in the end to simply an alias, because $args is really not equivalent to %*, in that it does not do parameter expansion. I think I need something like this:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp globexpand($args) }
globexpand is of course, a placeholder, for some kind of expansion/globbing routine, which I haven't been able to find yet in PowerShell.
try this:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp (join-path -Path $pwd -ChildPath $args[0]) }
$pwd is an automatic variable with the current working path as value
Edit:
function npp {
if ($args[0] -match '.:\\.+')
{
nppapp $args[0]
}
else
{
nppapp (join-path -Path $pwd -ChildPath $args[0]) }
}